<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ca">
	<id>https://docs.moodle.org/2x/ca/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Afhole</id>
	<title>MoodleDocs - Contribucions de l&amp;#039;usuari [ca]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/2x/ca/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Afhole"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/Especial:Contribucions/Afhole"/>
	<updated>2026-04-18T04:05:13Z</updated>
	<subtitle>Contribucions de l&amp;#039;usuari</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Usuari:Alastair_Hole&amp;diff=103322</id>
		<title>Usuari:Alastair Hole</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Usuari:Alastair_Hole&amp;diff=103322"/>
		<updated>2013-02-22T12:34:19Z</updated>

		<summary type="html">&lt;p&gt;Afhole: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Alastair Hole &amp;lt;a.f.hole@bath.ac.uk&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Senior Educational Software and Systems Developer&lt;br /&gt;
&lt;br /&gt;
Learning and Teaching Enhancement Office&lt;br /&gt;
&lt;br /&gt;
University of Bath&lt;br /&gt;
&lt;br /&gt;
+44 (0)1225 383576&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Usuari:Alastair_Hole&amp;diff=103321</id>
		<title>Usuari:Alastair Hole</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Usuari:Alastair_Hole&amp;diff=103321"/>
		<updated>2013-02-22T12:34:09Z</updated>

		<summary type="html">&lt;p&gt;Afhole: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Alastair Hole &amp;lt;a.f.hole@bath.ac.uk&amp;gt;&lt;br /&gt;
Senior Educational Software and Systems Developer&lt;br /&gt;
Learning and Teaching Enhancement Office&lt;br /&gt;
University of Bath&lt;br /&gt;
+44 (0)1225 383576&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Performance_recommendations&amp;diff=101035</id>
		<title>Performance recommendations</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Performance_recommendations&amp;diff=101035"/>
		<updated>2012-10-03T14:21:44Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* PHP performance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Performance}}&lt;br /&gt;
Moodle can be made to perform very well, at small usage levels or scaling up to many thousands of users. The factors involved in performance are basically the same as for any PHP-based database-driven system. When trying to optimize your server, try to focus on the factor which will make the most difference to the user. For example, if you have relatively more users browsing than accessing the database, look to improve the webserver performance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Obtain a baseline benchmark==&lt;br /&gt;
&lt;br /&gt;
Before attempting any optimization, you should obtain a baseline benchmark of the component of the system you are trying to improve. For Linux try [http://lbs.sourceforge.net/ LBS] and for Windows use the Performance Monitor. Once you have quantitative data about how your system is performing currently, you&#039;ll be able to determine if the change you have made has had any real impact.&lt;br /&gt;
&lt;br /&gt;
The overall aim of adjustments to improve performance is to use RAM (cacheing) and to reduce disk-based activity. It is especially important to try to eliminate swap file usage as much as you can. If your system starts swapping, this is a sign that you need more RAM. &lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;optimization order preference&#039;&#039;&#039; is usually: primary storage (more RAM), secondary storage (faster hard disks/improved hard disk configuration), processor (more and faster).&lt;br /&gt;
&lt;br /&gt;
==Scalability==&lt;br /&gt;
&lt;br /&gt;
Moodle&#039;s design (with clear separation of application layers) allows for strongly scalable setups. (Please check the list of [[Large installations|large Moodle installations]].)&lt;br /&gt;
&lt;br /&gt;
Large sites usually separate the web server and database onto separate servers, although for smaller installations this is typically not necessary.&lt;br /&gt;
&lt;br /&gt;
It is possible to load-balance a Moodle installation, for example by using more than one webserver. The separate webservers should query the same database and refer to the same filestore area, but otherwise the separation of the application layers is complete enough to make this kind of clustering feasible. Similarly, the database could be a cluster of servers (e.g. a MySQL cluster), but this is not an easy task and you should seek expert support, e.g. from a Moodle Partner.&lt;br /&gt;
&lt;br /&gt;
===Server cluster===&lt;br /&gt;
&lt;br /&gt;
Using Moodle forum discussions:&lt;br /&gt;
&lt;br /&gt;
*[http://moodle.org/mod/forum/discuss.php?d=57202 Moodle clustering]&lt;br /&gt;
*[http://moodle.org/mod/forum/discuss.php?d=44470 Software load balancing]&lt;br /&gt;
*[http://moodle.org/mod/forum/discuss.php?d=49986 TCP load balancing]&lt;br /&gt;
*[http://moodle.org/mod/forum/discuss.php?d=88214 Installation for 3000 simultaneous users]&lt;br /&gt;
&lt;br /&gt;
==Hardware configuration==&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: The fastest and most effective change that you can make to improve performance is to &#039;&#039;&#039;increase the amount of RAM on your web server&#039;&#039;&#039; - get as much as possible (e.g. 4GB or more). Increasing primary memory will reduce the need for processes to swap to disk and will enable your server to handle more users.&lt;br /&gt;
* Better performance is gained by obtaining the best &#039;&#039;&#039;processor capability&#039;&#039;&#039; you can, i.e. dual or dual core processors. A modern BIOS should allow you to enable hyperthreading, but check if this makes a difference to the overall performance of the processors by using a [http://en.wikipedia.org/wiki/Super_PI CPU benchmarking tool].&lt;br /&gt;
* If you can afford them, use &#039;&#039;&#039;SCSI hard disks&#039;&#039;&#039; instead of SATA drives. SATA drives will increase your system&#039;s CPU utilization, whereas SCSI drives have their own integrated processors and come into their own when you have multiple drives. If you must have SATA drives, check that your motherboard and the drives themselves support NCQ (Native Command Queuing).&lt;br /&gt;
* Purchase hard disks with a &#039;&#039;&#039;low seek time&#039;&#039;&#039;. This will improve the overall speed of your system, especially when accessing Moodle&#039;s reports.&lt;br /&gt;
* Size your &#039;&#039;&#039;swap file&#039;&#039;&#039; correctly. The general advice is to set it to 4 x physical RAM.&lt;br /&gt;
* Use a &#039;&#039;&#039;RAID disk system&#039;&#039;&#039;. Although there are many different RAID configurations you can create, the following generally works best:&lt;br /&gt;
** install a hardware RAID controller (if you can)&lt;br /&gt;
** the operating system and swap drive on one set of disks configured as RAID-1.&lt;br /&gt;
** Moodle, Web server and Database server on another set of disks configured as RAID-5.&lt;br /&gt;
* Use &#039;&#039;&#039;gigabit ethernet&#039;&#039;&#039; for improved latency and throughput. This is especially important when you have your webserver and database server separated out on different hosts.&lt;br /&gt;
* Check the settings on your &#039;&#039;&#039;network card&#039;&#039;&#039;. You may get an improvement in performance by increasing the use of buffers and transmit/receive descriptors (balance this with processor and memory overheads) and off-loading TCP checksum calculation onto the card instead of the OS.&lt;br /&gt;
*  Read this [http://moodle.org/mod/forum/discuss.php?d=68579 Case Study] on a server stress test with 300 users.  &lt;br /&gt;
* See this [http://elearning.sgu.ac.jp/doc/PT/ accompanying report] on network traffic and server loads.&lt;br /&gt;
* See the [[Moodle.org configuration]]&lt;br /&gt;
* Also see this SFSU presentation at Educause (using VMWare): [http://www.educause.edu/Resources/AnOpenSourceLMSforaMissionCrit/162843]&lt;br /&gt;
&lt;br /&gt;
==Operating System==&lt;br /&gt;
* You can use [http://en.wikipedia.org/wiki/Linux Linux](recommended), Unix-based, Windows or Mac OS X for the server &#039;&#039;&#039;operating system&#039;&#039;&#039;. *nix operating systems generally require less memory than Mac OS X or Windows servers for doing the same task as the server is configured with just a shell interface. Additionally Linux does not have licensing fees attached, but can have a big learning curve if you&#039;re used to another operating system. If you have a large number of processors running SMP, you may also want to consider using a highly tuned OS such as [http://en.wikipedia.org/wiki/Solaris_Operating_Environment Solaris].&lt;br /&gt;
* Check your own OS and &#039;&#039;&#039;vendor specific instructions&#039;&#039;&#039; for optimization steps.&lt;br /&gt;
** For Linux look at the [http://linuxperf.sourceforge.net/ Linux Performance Team] site. &lt;br /&gt;
** For Linux investigate the hdparm command, e.g. hdparm -m16 -d1 can be used to enable read/write on multiple sectors and DMA. Mount disks with the async and noatime options.&lt;br /&gt;
** For Windows set the sever to be optimized for network applications (Control Panel, Network Connections, LAN connection, Properties, File &amp;amp; Printer Sharing for Microsoft Networks, Properties, Optimization). You can also search the [http://technet.microsoft.com/ Microsoft TechNet site] for optimization documents.&lt;br /&gt;
&lt;br /&gt;
==Web server performance==&lt;br /&gt;
&lt;br /&gt;
Installing [http://www.mozilla.com/en-US/ Firefox] and the [https://addons.mozilla.org/en-US/firefox/addon/1843 firebug] extension will allow you to watch the time it takes for each page component to load. Also, the [https://addons.mozilla.org/en-US/firefox/addon/5369 Yslow] extension will evaluate your page against Yahoo&#039;s [http://www.skrenta.com/2007/05/14_rules_for_fast_web_pages_by_1.html 14 rules], full text [http://developer.yahoo.com/performance/rules.html Best Practices for Speeding Up Your Web Site], &amp;lt;strike&amp;gt;([http://video.yahoo.com/video/play?vid=1040890 video])&amp;lt;/strike&amp;gt; for fast loading websites.&lt;br /&gt;
&lt;br /&gt;
===PHP performance===&lt;br /&gt;
* You are strongly recommended to use a &#039;&#039;&#039;PHP accelerator&#039;&#039;&#039; to ease CPU load, such as [http://pecl.php.net/apc APC], [http://www.php-accelerator.co.uk/ PHPA], [http://trac.lighttpd.net/xcache/ Xcache], [http://sourceforge.net/projects/wincache WinCache] or [http://eaccelerator.net/ eAccelerator]. (Take care to choose a PHP accelerator that is known to work well with your version of PHP and note that Turck MMCache is [http://turckmmcache.exeprod.com/TheManifestoEnglish no longer maintained] and can cause failures with PHP 5). &lt;br /&gt;
* Improvements in read/write performance can be improved by putting the cached PHP pages on a [[TMPFS]] filesystem - but remember that you&#039;ll lose the cache contents when there is a power failure or the server is rebooted.&lt;br /&gt;
* Performance of PHP is better when installed as an &#039;&#039;&#039;Apache/IIS6 ISAPI module&#039;&#039;&#039; (rather than a CGI). IIS 7.0/7.5 (Windows Server 2008/R2) users should choose a FastCGI installation for best performance.&lt;br /&gt;
* Also check the &#039;&#039;&#039;memory_limit&#039;&#039;&#039; in php.ini, reduce it to 16M for Moodle version earlier than 1.7 ([http://moodle.org/mod/forum/discuss.php?d=39656 See this forum discussion]). For Moodle 1.7 or later, it is recommended that the value of memory_limit should be 40M. As of [http://www.php.net/ChangeLog-5.php PHP 5.2.1] the default value for the memory_limit directive is 128M.&lt;br /&gt;
* Also see [[PHP_settings_by_Moodle_version]]&lt;br /&gt;
&lt;br /&gt;
===Install HowTo===&lt;br /&gt;
* [http://2bits.com/articles/installing-php-apc-gnulinux-centos-5.html APC on CentOS 5.x (linux)]&lt;br /&gt;
* [http://fplanque.com/dev/linux/install-apc-php-cache-debian-lenny APC on Debian (linux)]&lt;br /&gt;
* [http://www.linuxtuts.net/211-installing-memcached-php5-memcache-module-debian-apache2.html MemCache module on Debian (Apache2 and PHP5) ]&lt;br /&gt;
* [http://noveckg.blogspot.com/2010/03/installing-memcached-on-centos-5x.html Installing Memcache on CentOS 5.x (linux)]&lt;br /&gt;
* [http://noveckg.blogspot.com/2010/02/installing-eaccelerator-cache-for-php.html Installing eAccelerator on CentOS 5.x (linux)]&lt;br /&gt;
* [https://docs.moodle.org/en/Installing_eAccelerator_In_Ubuntu_Server/ Installing eAccelerator on Ubuntu Server (linux)]&lt;br /&gt;
&lt;br /&gt;
===Apache performance===&lt;br /&gt;
* If you are using Apache on a Windows server, use the build from [http://www.apachelounge.com Apache Lounge] which is reported to have [http://moodle.org/mod/forum/discuss.php?d=93358 performance and stability improvements] compared to the official Apache download. Note that this is an unofficial build, so may not keep up with official releases.&lt;br /&gt;
* Set the &#039;&#039;&#039;MaxClients&#039;&#039;&#039; directive correctly. Use this formula to help (which uses 80% of available memory to leave room for spare):&lt;br /&gt;
 MaxClients = Total available memory * 80% / Max memory usage of apache process&lt;br /&gt;
:Memory usage of apache process is usually 10MB but Moodle can easily use up to 100MB per process, so a general rule of thumb is to divide your available memory in megabytes by 100 to get a conservative setting for MaxClients. You are quite likely to find yourself lowering the MaxClients from its default of 150 on a Moodle server. To get a more accurate estimate read the value from the shell command:&lt;br /&gt;
 #ps -ylC httpd --sort:rss&lt;br /&gt;
&lt;br /&gt;
:If you need to increase the value of &#039;&#039;&#039;MaxClients&#039;&#039;&#039; beyond 256, you will also need to set the &#039;&#039;&#039;ServerLimit&#039;&#039;&#039; directive. &lt;br /&gt;
&lt;br /&gt;
:&#039;&#039;&#039;Warning&#039;&#039;&#039;: Do not be tempted to set the value of MaxClients higher than your available memory as your server will consume more RAM than available and start to swap to disk. &lt;br /&gt;
* Consider reducing the &#039;&#039;&#039;number of modules&#039;&#039;&#039; that Apache loads in the httpd.conf file to the minumum necessary to reduce the memory needed. &lt;br /&gt;
* Use the &#039;&#039;&#039;latest version of Apache&#039;&#039;&#039; - Apache 2 has an improved memory model which reduces memory usage further.&lt;br /&gt;
* For Unix/Linux systems, consider lowering &#039;&#039;&#039;MaxRequestsPerChild&#039;&#039;&#039; in httpd.conf to as low as 20-30 (if you set it any lower the overhead of forking begins to outweigh the benefits). &lt;br /&gt;
* For a heavily loaded server, consider setting &#039;&#039;&#039;KeepAlive Off&#039;&#039;&#039; (do this only if your Moodle pages do not contain links to resources or uploaded images) or lowering the &#039;&#039;&#039;KeepAliveTimeout&#039;&#039;&#039; to between 2 and 5. The default is 15 (seconds) - the higher the value the more server processes will be kept waiting for possibly idle connections. A more accurate value for KeepAliveTimeout is obtained by observing how long it takes your users to download a page. After altering any of the KeepAlive variables, monitor your CPU utilization as there may be an additional overhead in initiating more worker processes/threads.&lt;br /&gt;
* As an alternative to using KeepAlive Off, consider setting-up a &#039;&#039;&#039;Reverse Proxy server&#039;&#039;&#039; infront of the Moodle server to cache HTML files with images. You can then return Apache to using keep-alives on the Moodle server.&lt;br /&gt;
* If you do not use a .htaccess file, set the &#039;&#039;&#039;AllowOverride&#039;&#039;&#039; variable to AllowOverride None to prevent .htaccess lookups.&lt;br /&gt;
* Set &#039;&#039;&#039;DirectoryIndex&#039;&#039;&#039; correctly so as to avoid content-negotiation. Here&#039;s an example from a production server:&lt;br /&gt;
 DirectoryIndex index.php index.html index.htm&lt;br /&gt;
* Unless you are doing development work on the server, set &#039;&#039;&#039;ExtendedStatus Off&#039;&#039;&#039; and disable mod_info as well as mod_status.&lt;br /&gt;
* Leave &#039;&#039;&#039;HostnameLookups Off&#039;&#039;&#039; (as default) to reduce DNS latency.&lt;br /&gt;
* Consider reducing the value of &#039;&#039;&#039;TimeOut&#039;&#039;&#039; to between 30 to 60 (seconds). &lt;br /&gt;
* For the &#039;&#039;&#039;Options directive&#039;&#039;&#039;, avoid Options Multiviews as this performs a directory scan. To reduce disk I/O further use&lt;br /&gt;
 Options -Indexes FollowSymLinks&lt;br /&gt;
*&#039;&#039;&#039;Caching (unsupported)&#039;&#039;&#039; - &#039;&#039;Please note that this kind of caching may create major problems during upgrades.&#039;&#039; Apache can be told to make pages load a lot faster by specifying that the browser should cache some various page elements such as images and reuse them from local memory rather than ask for them again every time a page is requested. How to do this varies slightly between OSes but there are two basic steps:&lt;br /&gt;
&lt;br /&gt;
# Install and enable mod_expires - refer to documentation or man pages&lt;br /&gt;
# Add this code to the virtual server config file within the &amp;lt;directory&amp;gt; section for the root directory (or within the .htaccess file if AllowOverrides is On):&lt;br /&gt;
 &amp;lt;IfModule mod_expires.c&amp;gt;&lt;br /&gt;
  ExpiresActive On&lt;br /&gt;
  ExpiresDefault &amp;quot;access plus 1 seconds&amp;quot;&lt;br /&gt;
  ExpiresByType text/html &amp;quot;access plus 1 seconds&amp;quot;&lt;br /&gt;
  ExpiresByType image/gif &amp;quot;access plus 1 week&amp;quot;&lt;br /&gt;
  ExpiresByType image/jpeg &amp;quot;access plus 1 week&amp;quot;&lt;br /&gt;
  ExpiresByType image/png &amp;quot;access plus 1 week&amp;quot;&lt;br /&gt;
  ExpiresByType text/css &amp;quot;access plus 1 week&amp;quot;&lt;br /&gt;
  ExpiresByType text/javascript &amp;quot;access plus 1 week&amp;quot;&lt;br /&gt;
  ExpiresByType application/x-javascript &amp;quot;access plus 1 week&amp;quot;&lt;br /&gt;
  ExpiresByType text/xml &amp;quot;access plus 1 seconds&amp;quot;&lt;br /&gt;
 &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The effect is to make everything stay in the cache except HTML and XML, which change dynamically. It&#039;s possible to gain a several hundred percent decrease in load times this way. Adjust the cache times according to how often your images etc change.&lt;br /&gt;
&lt;br /&gt;
* Compression reduces response times by reducing the size of the HTTP response&lt;br /&gt;
# Install and enable mod_deflate - refer to documentation or man pages&lt;br /&gt;
# Add this code to the virtual server config file within the &amp;lt;directory&amp;gt; section for the root directory (or within the .htaccess file if AllowOverrides is On):&lt;br /&gt;
 &amp;lt;ifModule mod_deflate.c&amp;gt;&lt;br /&gt;
   AddOutputFilterByType DEFLATE text/html text/plain text/xml&lt;br /&gt;
 &amp;lt;/ifmodule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
More info: [http://www.metaskills.net/blog/heuristics/sysadmin/how-to-control-browser-caching-with-apache-2 www.metaskills.net]&lt;br /&gt;
&lt;br /&gt;
===IIS performance===&lt;br /&gt;
All alter this location in the registry:&lt;br /&gt;
 HKLM\SYSTEM\CurrentControlSet\Services\Inetinfo\Parameters\&lt;br /&gt;
* The equivalent to KeepAliveTimeout is &#039;&#039;&#039;ListenBackLog&#039;&#039;&#039; (IIS - registry location is HKLM\ SYSTEM\ CurrentControlSet\ Services\ Inetinfo\ Parameters). Set this to between 2 to 5.&lt;br /&gt;
*Change the &#039;&#039;&#039;MemCacheSize&#039;&#039;&#039; value to adjust the amount of memory (Mb) that IIS will use for its file cache (50% of available memory by default).&lt;br /&gt;
*Change the &#039;&#039;&#039;MaxCachedFileSize&#039;&#039;&#039; to adjust the maximum size of a file cached in the file cache in bytes. Default is 262,144 (256K).&lt;br /&gt;
*Create a new DWORD called &#039;&#039;&#039;ObjectCacheTTL&#039;&#039;&#039; to change the length of time (in milliseconds) that objects in the cache are held in memory. Default is 30,000 milliseconds (30 seconds).&lt;br /&gt;
&lt;br /&gt;
===Lighttpd, NginX and Cherokee performance===&lt;br /&gt;
You can increase server performance by using a &#039;&#039;&#039;light-weight&#039;&#039;&#039; webserver like [http://www.lighttpd.net/ lighttpd],  [http://nginx.net/ nginx] or [http://www.cherokee-project.com/ cherokee] in combination with PHP in FastCGI-mode. Lighttpd was originally created as a proof-of-concept[http://www.lighttpd.net/story] to address the [http://www.kegel.com/c10k.html C10k problem] and while primarily recommended for memory-limited servers, its design origins and asynchronous-IO model make it a suitable and proven[http://blog.lighttpd.net/articles/2006/12/28/lighttpd-powers-5-alexa-top-250-sites] alternative HTTP server for high-load websites and web apps, including Moodle. See the [[lighttpd | MoodleDocs Lighttpd page]] for additional information, configuration example and links.&lt;br /&gt;
&lt;br /&gt;
Alternatively, both [http://www.lighttpd.net/ lighttpd] and [http://nginx.net/ nginx] are capable of performing as a load-balancer and/or reverse-proxy to alleviate load on back-end servers[http://www.linuxjournal.com/article/10108], providing benefit without requiring an actual software change on existing servers.&lt;br /&gt;
&lt;br /&gt;
Do note that these are likely to be the least tested server environments of all particularly if you are using advanced features such as web services and/or Moodle Networking. They are probably best considered for heavily used Moodle sites with relatively simple configurations.&lt;br /&gt;
&lt;br /&gt;
==Database performance==&lt;br /&gt;
&lt;br /&gt;
Moodle contains a script which will display some key database performance statistics from the [http://phplens.com/lens/adodb/docs-perf.htm ADOdb performance monitor]. Run the script in your browser as in the following example:&lt;br /&gt;
&lt;br /&gt;
 http://www.mymoodle.com/admin/dbperformance.php&lt;br /&gt;
&lt;br /&gt;
Use the data displayed as a guide to tune and improve the performance of your database server.&lt;br /&gt;
&lt;br /&gt;
===MySQL performance===&lt;br /&gt;
&lt;br /&gt;
The following are MySQL specific settings which can be adjusted for better performance in your my.cnf (my.ini in Windows). The file contains a list of settings and their values. To see the current values use these commands&lt;br /&gt;
 SHOW STATUS;&lt;br /&gt;
 SHOW VARIABLES; &lt;br /&gt;
&#039;&#039;&#039;Important&#039;&#039;&#039;: You must make backups of your database before attempting to change any MySQL server configuration. After any change to the my.cnf, restart mysqld.&lt;br /&gt;
&lt;br /&gt;
If you are able, the [http://mysqltuner.com/ MySQLTuner] tool can be run against your MySQL server and will calculate appropriate configuration values for most of the following settings based on your current load, status and variables automatically.&lt;br /&gt;
&lt;br /&gt;
* Enable the &#039;&#039;&#039;query cache&#039;&#039;&#039; with &lt;br /&gt;
 query_cache_type = 1. &lt;br /&gt;
For most Moodle installs, set the following:&lt;br /&gt;
 query_cache_size = 36M &lt;br /&gt;
 query_cache_min_res_unit = 2K. &lt;br /&gt;
The query cache will improve performance if you are doing few updates on the database. &lt;br /&gt;
* Set the &#039;&#039;&#039;table cache&#039;&#039;&#039; correctly. For Moodle 1.6 set &lt;br /&gt;
 table_cache = 256 #(table_open_cache in MySQL &amp;gt; 5.1.2)&lt;br /&gt;
(min), and for Moodle 1.7 set &lt;br /&gt;
 table_cache = 512 #(table_open_cache in MySQL &amp;gt; 5.1.2)&lt;br /&gt;
(min). The table cache is used by all threads (connections), so monitor the value of opened_tables to further adjust - if opened_tables &amp;gt; 3 * table_cache(table_open_cache in MySQL &amp;gt; 5.1.2) then increase table_cache upto your OS limit. Note also that the figure for table_cache will also change depending on the number of modules and plugins you have installed. Find the number for your server by executing the mysql statement below. Look at the number returned and set table_cache to this value.&lt;br /&gt;
 mysql&amp;gt;SELECT COUNT(table_name) FROM information_schema.tables WHERE table_schema=&#039;yourmoodledbname&#039;;&lt;br /&gt;
* Set the &#039;&#039;&#039;thread cache&#039;&#039;&#039; correctly. Adjust the value so that your thread cache utilization is as close to 100% as possible by this formula:&lt;br /&gt;
 thread cache utilization (%) = (threads_created / connections) * 100&lt;br /&gt;
* The &#039;&#039;&#039;key buffer&#039;&#039;&#039; can improve the access speed to Moodle&#039;s SELECT queries. The correct size depends on the size of the index files (.myi) and in Moodle 1.6 or later (without any additional modules and plugins), the recommendation for this value is key_buffer_size = 32M. Ideally you want the database to be reading once from the disk for every 100 requests so monitor that the value is suitable for your install by adjusting the value of key_buffer_size so that the following formulas are true:&lt;br /&gt;
 key_read / key_read_requests &amp;lt; 0.01&lt;br /&gt;
 key_write / key_write_requests &amp;lt;= 1.0&lt;br /&gt;
* Set the &#039;&#039;&#039;maximum number of connections&#039;&#039;&#039; so that your users will not see a &amp;quot;Too many connections&amp;quot; message. Be careful that this may have an impact on the total memory used. MySQL connections usually last for milliseconds, so it is unusual even for a heavily loaded server for this value to be over 200.&lt;br /&gt;
* Manage &#039;&#039;&#039;high burst activity&#039;&#039;&#039;. If your Moodle install uses a lot of quizzes and you are experiencing performance problems (check by monitoring the value of threads_connected - it should not be rising) consider increasing the value of back_log.&lt;br /&gt;
* &#039;&#039;&#039;Optimize your tables weekly and after upgrading Moodle&#039;&#039;&#039;. It is good practice to also optimize your tables after performing a large data deletion exercise, e.g. at the end of your semester or academic year. This will ensure that index files are up to date. Backup your database first and then use:&lt;br /&gt;
 mysql&amp;gt;CHECK TABLE mdl_tablename;&lt;br /&gt;
 mysql&amp;gt;OPTIMIZE TABLE mdl_tablename;&lt;br /&gt;
:The common tables in Moodle to check are mdl_course_sections, mdl_forum_posts, mdl_log and mdl_sessions (if using dbsessions). Any errors need to be corrected using REPAIR TABLE (see the [http://dev.mysql.com/doc/refman/5.0/en/repair-table.html MySQL manual] and this [http://moodle.org/mod/forum/discuss.php?d=58208#p279638 forum script]).&lt;br /&gt;
* &#039;&#039;&#039;Maintain the key distribution&#039;&#039;&#039;. Every month or so it is a good idea to stop the mysql server and run these myisamchk commands.&lt;br /&gt;
 #myisamchk -a -S /pathtomysql/data/moodledir/*.MYI&lt;br /&gt;
:&#039;&#039;&#039;Warning&#039;&#039;&#039;: You must stop the mysql database process (mysqld) before running any myisamchk command. If you do not, you risk data loss.&lt;br /&gt;
* Reduce the number of &#039;&#039;&#039;temporary tables saved to disk&#039;&#039;&#039;. Check this with the created_tmp_disk_tables value. If this is relatively large (&amp;gt;5%) increase tmp_table_size until you see a reduction. Note that this will have an impact on RAM usage.&lt;br /&gt;
&lt;br /&gt;
===PostgreSQL performance===&lt;br /&gt;
&lt;br /&gt;
There are some good papers around on tuning PostgreSQL (like [http://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server this one]), and Moodle&#039;s case does not seem to be different to the general case.&lt;br /&gt;
&lt;br /&gt;
The first thing to recognise is that if you really need to worry about tuning you should be using a separate machine for the database server. If you are not using a separate machine then the answers to many performance questions are substantially muddied by the memory requirements of the rest of the application.&lt;br /&gt;
&lt;br /&gt;
You should probably &#039;&#039;&#039;enable autovacuum&#039;&#039;&#039;, unless you know what you are doing. Many e-learning sites have predictable periods of low use, so disabling autovacuum and running a specific vacuum at those times can be a good option. Or perhaps leave autovacuum running but do a full vacuum weekly in a quiet period.&lt;br /&gt;
&lt;br /&gt;
Set &#039;&#039;&#039;shared_buffers&#039;&#039;&#039; to something reasonable. For versions up to 8.1 my testing has shown that peak performance is almost always obtained with buffers &amp;lt; 10000, so if you are using such a version, and have more than 512M of RAM just set shared_buffers to 10,000 (8MB).&lt;br /&gt;
&lt;br /&gt;
The buffer management had a big overhaul in 8.2 and &amp;quot;reasonable&amp;quot; is now a much larger number. I have not conducted performance tests with 8.2, but the recommendations from others are generally that you should now scale shared_buffers much more with memory and may continue to reap benefits even up to values like 100,000 (80MB). Consider using 1-2% of system RAM.&lt;br /&gt;
&lt;br /&gt;
PostgreSQL will also assume that the operating system is caching its files, so setting &#039;&#039;&#039;effective_cache_size&#039;&#039;&#039; to a reasonable value is also a good idea. A reasonable value will usually be (total RAM - RAM in use by programs). If you are running Linux and leave the system running for a day or two you can look at &#039;free&#039; and under the &#039;cached&#039; column you will see what it currently is. Consider taking that number (which is kB) and dividing it by 10 (i.e. allow 20% for other programs cache needs and then divide by 8 to get pages). If you are not using a dedicated database server you will need to decrease that value to account for usage by other programs.&lt;br /&gt;
&lt;br /&gt;
Some other useful parameters that can have positive effects, and the values I would typically set them to on a machine with 4G RAM, are:&lt;br /&gt;
&lt;br /&gt;
 work_mem = 10240&lt;br /&gt;
&lt;br /&gt;
That&#039;s 10M of RAM to use instead of on-disk sorting and so forth. That can give a big speed increase, but it is per connection and 200 connections * 10M is 2G, so it can theoretically chew up a lot of RAM.&lt;br /&gt;
&lt;br /&gt;
 maintenance_work_mem = 163840&lt;br /&gt;
&lt;br /&gt;
That&#039;s 160M of RAM which will be used by (e.g.) VACUUM, index rebuild, cluster and so forth. This should only be used periodically and should be freed when those processes exit, so I believe it is well worth while.&lt;br /&gt;
&lt;br /&gt;
 max_fsm_pages = 100000&lt;br /&gt;
 max_fsm_relations = 5000&lt;br /&gt;
&lt;br /&gt;
These are used to hold the free-space map, and if they are too small you will see performance degradation after the database has been operating for some time. The exact numbers to set can be gleaned from the output of VACUUM VERBOSE, which prints the required FSM pages at the end of it&#039;s run. The 5x increase seems to be useful for a Moodle installation, from experience.&lt;br /&gt;
&lt;br /&gt;
 wal_buffers = 64&lt;br /&gt;
&lt;br /&gt;
These buffers are used for the write-ahead log, and there have been a number of reports on the PostgreSQL mailing lists of improvement from this level of increase.&lt;br /&gt;
&lt;br /&gt;
This is a little out of date now (version 8.0) but still worth a read: http://www.powerpostgresql.com/Docs&lt;br /&gt;
&lt;br /&gt;
And there is lots of good stuff here as well: http://www.varlena.com/GeneralBits/Tidbits/index.php&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Based on Andrew McMillan&#039;s post at [http://moodle.org/mod/forum/discuss.php?d=68558 Tuning PostgreSQL] forum thread.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
===Other database performance links===&lt;br /&gt;
* Consider using a &#039;&#039;&#039;distributed cacheing system&#039;&#039;&#039; like [http://en.wikipedia.org/wiki/Memcached memcached] but note that memcached does not have any security features so it should be used behind a firewall.&lt;br /&gt;
* Consider using PostgreSQL. See [[Arguments in favour of PostgreSQL]] and [http://moodle.org/mod/forum/discuss.php?d=49195 how to migrate from MySQL to PostgreSQL] (forum discussion).&lt;br /&gt;
* [[Increasing the database connection lifetime | Try increasing the database connection lifetime]]&lt;br /&gt;
* [http://dev.mysql.com/doc/refman/5.0/en/server-parameters.html General advice on tuning MySQL parameters] (advice from the MySQL manual)&lt;br /&gt;
* [http://www.mysqlperformanceblog.com/2007/11/01/innodb-performance-optimization-basics/ InnoDB performance optimization] taken from the [http://www.mysqlperformanceblog.com/ MySQL performance blog] site.&lt;br /&gt;
&lt;br /&gt;
==Performance of different Moodle modules==&lt;br /&gt;
&lt;br /&gt;
Moodle&#039;s activity modules, filters, and other plugins can be activated/deactivated. If necessary, you may wish to deactivate some features (such as chat) if not required - but this isn&#039;t necessary. Some notes on the performance of certain modules:&lt;br /&gt;
&lt;br /&gt;
* The &#039;&#039;&#039;Chat&#039;&#039;&#039; module is [http://moodle.org/mod/forum/discuss.php?d=37979&amp;amp;parent=175079 said] to be a hog in terms of frequent HTTP requests to the main server. This can be reduced by setting the module to use &#039;&#039;Streamed&#039;&#039; updates, or, if you&#039;re using a Unix-based webserver, by running the chat in daemon mode. When using the Chat module use the configuration settings to tune for your expected load. Pay particular attention to the &#039;&#039;chat_old_ping&#039;&#039; and &#039;&#039;chat_refresh&#039;&#039; parameters as these can have greatest impact on server load.&lt;br /&gt;
* The &#039;&#039;&#039;Quiz&#039;&#039;&#039; module is known to stretch database performance. Try to optimise your database server by tuning. See [http://moodle.org/mod/forum/discuss.php?d=25616&amp;amp;parent=120770 for a brief report on performance for 55 students simultaneously using quizzes]&lt;br /&gt;
** See this Case Study for an extensive server stress test with 300 quiz users.[http://moodle.org/mod/forum/discuss.php?d=68579]  And this accompanying report on network traffic and server loads. [http://elearning.sgu.ac.jp/doc/PT/]&lt;br /&gt;
* The Moodle &#039;&#039;&#039;Cron&#039;&#039;&#039; task is triggered by calling the script &#039;&#039;cron.php&#039;&#039;. If this is called over HTTP (e.g. using wget or curl) it can take a large amount of memory on large installations. If it is called by directly invoking the php command (e.g. &#039;&#039;php -f /path/to/moodle/directory/admin/cron.php&#039;&#039;) efficiency can be much improved.&lt;br /&gt;
* The &#039;&#039;&#039;Recent activities&#039;&#039;&#039; block is consuming to much resources if you have huge number of records &amp;lt;code&amp;gt;mdl_log&amp;lt;/code&amp;gt;. this is being tested to optimize the SQL query.&lt;br /&gt;
&lt;br /&gt;
==Moodle Image Optimization==&lt;br /&gt;
&lt;br /&gt;
The base images delivered in the original Moodle distribution package provide unoptimized graphics, most of which can benefit from lossless recompression utilizing [http://optipng.sourceforge.net/ optipng] for PNGs, [http://www.lcdf.org/gifsicle/ gifsicle] for GIFs and [http://www.kokkonen.net/tjko/projects.html jpegoptim] for JPGs.  Optimized graphics transfer faster and provide a faster perceived response for clients[http://www.websiteoptimization.com/speed/12/], especially distance learners.  The following example will recursively optimize (without any loss of quality) all the graphics and image files included in a base Moodle installation directory on a server with the above commands installed and available.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
find /example/directory/moodle-1.9 -iname *.png -exec optipng -o7 {} \;&lt;br /&gt;
find /example/directory/moodle-1.9 -iname *.gif -exec gifsicle -O2 -b {} \;&lt;br /&gt;
find /example/directory/moodle-1.9 -iname *.jpg -exec jpegoptim -p {} \;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Both [http://optipng.sourceforge.net/ optipng] and [http://www.lcdf.org/gifsicle/ gifsicle] are provided in the base repositories of most newer Linux distributions; [http://www.kokkonen.net/tjko/projects.html jpegoptim] must be downloaded and installed manually.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
*Using Moodle: [http://moodle.org/mod/forum/view.php?f=94 Hardware and Performance] forum&lt;br /&gt;
&lt;br /&gt;
There have been a lot of discussions on moodle.org about performance, here are some of the more interesting and (potentially) useful ones:&lt;br /&gt;
&lt;br /&gt;
* [http://moodle.org/mod/forum/discuss.php?d=83057 Performance woes!]&lt;br /&gt;
* [http://moodle.org/mod/forum/discuss.php?d=57028 Performance perspectives - a little script]&lt;br /&gt;
* [http://moodle.org/mod/forum/discuss.php?d=88927 Comments on planned server hardware]&lt;br /&gt;
* [http://moodle.org/mod/forum/discuss.php?d=102978#p461624 Moodle performance in a pil by Martin Langhoff]&lt;br /&gt;
&lt;br /&gt;
[[es:Rendimiento]]&lt;br /&gt;
[[fr:Performance]]&lt;br /&gt;
[[ja:パフォーマンス]]&lt;br /&gt;
[[de:Geschwindigkeitsempfehlungen]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/JavaScript_guidelines&amp;diff=74816</id>
		<title>Broken/JavaScript guidelines</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/JavaScript_guidelines&amp;diff=74816"/>
		<updated>2010-08-16T19:41:14Z</updated>

		<summary type="html">&lt;p&gt;Afhole: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 2.0}}&lt;br /&gt;
&lt;br /&gt;
These guidelines can only be applied fully from Moodle 2.0 onwards, because they rely on our new API to facilitate use of JavaScript.&lt;br /&gt;
&lt;br /&gt;
When writing JavaScript for earlier versions of Moodle, please try to follow these guidelines in spirit and use the &#039;&#039;&#039;require_js&#039;&#039;&#039; function in place of $PAGE-&amp;gt;requires-&amp;gt;js_module/yui2_lib.&lt;br /&gt;
&lt;br /&gt;
==General principles==&lt;br /&gt;
&lt;br /&gt;
===Moodle should be usable without JavaScript===&lt;br /&gt;
&lt;br /&gt;
Everything in Moodle should work with JavaScript turned off. This is important for accessibility, and in line with the principles of [[Development:Unobtrusive_Javascript|unobtrusive JavaScript]] and [[Development:Progressive_enhancement|progressive enhancement]].&lt;br /&gt;
&lt;br /&gt;
===Minimise inline JavaScript===&lt;br /&gt;
&lt;br /&gt;
Almost all JavaScript code should be in separate .js files. There should be the smallest possible amount of JavaScript inline in the HTML code of pages.&lt;br /&gt;
&lt;br /&gt;
The only &amp;amp;lt;script&amp;gt; tags in the HTML should be&lt;br /&gt;
# &amp;amp;lt;script src=... tags to include the necessary .js files.&lt;br /&gt;
# Simple function calls to trigger initialisation and pass data from PHP to JavaScript. For example &amp;amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;initialise_my_widget(&#039;some&#039;, &#039;data&#039;, 123);&amp;amp;lt;/script&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Even these small amounts of JS you should not output directly. They will be generated automatically by calls to the $PAGE-&amp;gt;requries-&amp;gt;js_init_call() API. See below for details.&lt;br /&gt;
&lt;br /&gt;
You should not use old-fashioned onXXX=&amp;quot;event_handler&amp;quot; attributes in the HTML. Use Modern DOM events. The YUI events library makes this easy.&lt;br /&gt;
&lt;br /&gt;
=== JavaScript libraries ===&lt;br /&gt;
&lt;br /&gt;
* The official JavaScript library for Moodle is [[Development:YUI|YUI]]. That may not be your favourite, but it&#039;s the one that was chosen after careful research, so live with it.&lt;br /&gt;
&lt;br /&gt;
* Moodle also has its own JavaScript library code, packaged into in various JavaScript modules.&lt;br /&gt;
&lt;br /&gt;
* Moodle uses the &#039;&#039;&#039;TinyMCE&#039;&#039;&#039; HTML editor.&lt;br /&gt;
&lt;br /&gt;
===When to include the JavaScript===&lt;br /&gt;
&lt;br /&gt;
As per [http://developer.yahoo.com/performance/rules.html Yahoo&#039;s best practice guidelines], load and execute the JavaScript as late as possible, ideally the script tags should be the last thing before the &amp;amp;lt;/body&amp;gt; close tag. (This is the default behaviour with Moodle&#039;s JavaScript handling functions like $PAGE-&amp;gt;requries-&amp;gt;js_init_call().)&lt;br /&gt;
&lt;br /&gt;
Since everything should work without JavaScript, load and initialising your scripts only after everything else on the page has loaded should not be a problem and will increase the perceived page-load performance for users.&lt;br /&gt;
&lt;br /&gt;
===Minimise the number of .js files===&lt;br /&gt;
&lt;br /&gt;
Try not to use too many different .js files. Each separate file that the browser has to load incurs an overhead.&lt;br /&gt;
&lt;br /&gt;
On the other hand, organise the JavaScript logically to ease maintenance, and don&#039;t include large amounts of irrelevant JavaScript code. Code that is loaded but never used is a waste of time.&lt;br /&gt;
&lt;br /&gt;
So, if you are writing a new module that needs its own JavaScript, try starting with a single file mod/mymod/module.js. If you find that you are writing a lot of JavaScript that is only needed when the teacher edits your module, but is not needed by students, then consider splitting that code into a separate file like mod/mymod/edit.js, and only including it where needed.&lt;br /&gt;
&lt;br /&gt;
==How to achive these general principles in Moodle==&lt;br /&gt;
&lt;br /&gt;
The rest of this page explains how you can achieve the above goals.&lt;br /&gt;
&lt;br /&gt;
===Getting Moodle to load your JavaScript files===&lt;br /&gt;
&lt;br /&gt;
Everything required by the current page is tracked by the $PAGE-&amp;gt;requires object, which is an instance of the [http://phpdocs.moodle.org/HEAD/moodlecore/page_requirements_manager.html page_requirements_manager] class defined in lib/outputrequirementslib.php.&lt;br /&gt;
&lt;br /&gt;
The most important method in this class is the -&amp;gt;js_init_call(...) method. You use it like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;js_init_call(&#039;M.mod_mymod.init_something&#039;, array(&#039;some&#039;, &#039;data&#039;, &#039;from&#039;, &#039;PHP&#039;));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that this will implicitly load the JavaScript code in mod/mymod/module.js, and then call the M.mod_mymod.init_something function passing four string arguments &#039;some&#039;, &#039;data&#039;, &#039;from&#039;, &#039;PHP&#039;. You can pass any PHP type as an argument. The PHP values are encoded with json_encode before being passed to JavaScript, so numbers, strings, arrays and objects all work.&lt;br /&gt;
&lt;br /&gt;
Sometimes, the code in the JavaScript module mod/mymod/module.js may require some other JavaScript libraries to be loaded, or it may require some language strings. In that case you need to use the full form of js_init_call:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$jsmodule = array(&lt;br /&gt;
    &#039;name&#039;     =&amp;gt; &#039;mod_mymod&#039;,&lt;br /&gt;
    &#039;fullpath&#039; =&amp;gt; &#039;/mod/mymod/module.js&#039;,&lt;br /&gt;
    &#039;requires&#039; =&amp;gt; array(&#039;base&#039;, &#039;io&#039;, &#039;node&#039;, &#039;json&#039;),&lt;br /&gt;
    &#039;strings&#039; =&amp;gt; array(&lt;br /&gt;
        array(&#039;something&#039;, &#039;mymod&#039;),&lt;br /&gt;
        array(&#039;confirmdelete&#039;, &#039;mymod&#039;),&lt;br /&gt;
        array(&#039;yes&#039;, &#039;moodle&#039;),&lt;br /&gt;
        array(&#039;no&#039;, &#039;moodle&#039;)&lt;br /&gt;
    )&lt;br /&gt;
);&lt;br /&gt;
$PAGE-&amp;gt;requires-&amp;gt;js_init_call(&#039;M.mod_mymod.init_something&#039;, array(&#039;some&#039;, &#039;data&#039;, &#039;from&#039;, &#039;PHP&#039;), false, $jsmodule);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
(Naturally, it would be a good idea to put the definition of the $jsmodule array somewhere central like in the locallib.php file.)&lt;br /&gt;
&lt;br /&gt;
The libraries in the &#039;requires&#039; sub-array are YUI3 (or YUI2) libraries.&lt;br /&gt;
&lt;br /&gt;
The strings are Moodle language strings. In order for Moodle to have correctly determined the user&#039;s language, you should only call js_init_call after the call to require_login, which sets the current course, and ensures the user is logged in.&lt;br /&gt;
&lt;br /&gt;
$PAGE-&amp;gt;requires keeps track of which files have been included. For example if two other modules both require the YUI base module, then it is only included once.&lt;br /&gt;
&lt;br /&gt;
===JavaScript coding style===&lt;br /&gt;
&lt;br /&gt;
Moodle JavaScript code should should follow the same [[Development:Coding_style|coding style as Moodle PHP code]], allowing for the differences between PHP and JavaScript.&lt;br /&gt;
&lt;br /&gt;
For example, all the rules on &#039;&#039;function_names&#039;&#039;, &#039;&#039;class_names&#039;&#039; and &#039;&#039;variablenames&#039;&#039; apply. You should document your code with [http://jsdoc.sourceforge.net/ JSDoc] comments. Layout your JavaScript expressions and statements like the equivalent PHP ones.&lt;br /&gt;
&lt;br /&gt;
Normally, your .js files should simply define things like functions, classes and variables. When the file is loaded, no JavaScript code should actually be executed that has any effect unless it is the sort of code that can safely be executed once per HTML page. This is so that it plays nicely with the require_once-like behaviour of $PAGE-&amp;gt;requires-&amp;gt;js.&lt;br /&gt;
&lt;br /&gt;
Do not pollute the global JavaScript name-space. Try to package your JavaScript into objects, and put all objects inside the global M object, like the M.mod_mymod example above. &lt;br /&gt;
&lt;br /&gt;
===Different content when JavaScript is on or off===&lt;br /&gt;
&lt;br /&gt;
Remember the overriding principals that everything should work with JavaScript off, and we should adopt a [[Progressive enhancement]] approach. However, there are valid reasons why sometimes you need different content with JavaScript is on or off. We can break it down into three cases:&lt;br /&gt;
&lt;br /&gt;
====Content that should only be visible with JavaScript off====&lt;br /&gt;
&lt;br /&gt;
An example of this is the automatic search when you are looking for a user to assign a role to. With JavaScript on, the search automatically starts after a delay. With JavaScript off, we want an explicit Search button visible.&lt;br /&gt;
&lt;br /&gt;
To handle this case, Moodle automatically add a class &#039;jsenabled&#039; to the body tag using JavaScript. So you just need to add a rule like&lt;br /&gt;
&amp;lt;code css&amp;gt;&lt;br /&gt;
body.jsenebled .mywidget .submitbutton {&lt;br /&gt;
    display: none;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
to the stylesheet, and the button will be invisible if JavaScript is enabled.&lt;br /&gt;
&lt;br /&gt;
An alternative strategy is to remove the particular bits of HTML from the page using DOM methods. However, if your JavaScript is only loaded at the end of the page, it may take some time for the extra content to disappear, which leads to a disconcerting flicker in the page.&lt;br /&gt;
&lt;br /&gt;
Yet another approach is the old-fashioned &amp;lt;noscript&amp;gt; tag.&lt;br /&gt;
&lt;br /&gt;
====Content that needs to be visible right away when JavaScript is on====&lt;br /&gt;
&lt;br /&gt;
An example is the &amp;lt;nowiki&amp;gt;[+] or [-]&amp;lt;/nowiki&amp;gt; icon that can be used to expand/collapse each block if JavaScript is on.&lt;br /&gt;
&lt;br /&gt;
We can divide this into two subcases:&lt;br /&gt;
&lt;br /&gt;
=====Content generated by PHP code=====&lt;br /&gt;
&lt;br /&gt;
Where the HTML for the JavaScript only widget is generated by PHP, we can make it invisible when JavaScript is off using just CSS:&lt;br /&gt;
&amp;lt;code css&amp;gt;&lt;br /&gt;
.mywidget {&lt;br /&gt;
    display: none;&lt;br /&gt;
}&lt;br /&gt;
body.jsenabled .mywidget {&lt;br /&gt;
    display: block;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
However, it could be argued that this approach is not really progressive enhancement.&lt;br /&gt;
&lt;br /&gt;
=====Content generated by JavaScript code=====&lt;br /&gt;
&lt;br /&gt;
This is more in keeping with progressive enhancement, and this is the way that the expand/collapse block icon is handled.&lt;br /&gt;
&lt;br /&gt;
We build the icon using DOM methods. The only problem is that as the JavaScript is loaded in the footer, there is a small delay before the icons appear. Since when the icons appear, they do not cause other content on the page to move around, that is OK. Also, this delayed appearance is becoming more common on the web. For example, on http://twitter.com/, some things only appear a moment after the main part of the page has finished loading.&lt;br /&gt;
&lt;br /&gt;
However, it the delayed appearance is really a problem, then the only solution is to embed the JavaScript that generates the extra content in the middle of the HTML, using the js_writer class.&lt;br /&gt;
&lt;br /&gt;
====Content that only appears when the user does something, when JavaScript is on====&lt;br /&gt;
&lt;br /&gt;
An example of this is something like file picker dialog that appears when you add an image to some content in the HTML editor, or the one that pops up when you click &#039;Add new question&#039; in the quiz editing interface.&lt;br /&gt;
&lt;br /&gt;
We have the same two sub-cases:&lt;br /&gt;
&lt;br /&gt;
=====Content generated by PHP code=====&lt;br /&gt;
&lt;br /&gt;
In this case, you need to make sure the content is always covered by a &#039;&#039;display: none;&#039;&#039; rule in the CSS, but then when the user takes an action like clicking a button to reveal the extra content, you need to override that class name some how, perhaps by adding or removing a className using JavaScript.&lt;br /&gt;
&lt;br /&gt;
=====Content generated by JavaScript code=====&lt;br /&gt;
&lt;br /&gt;
In this case, there is no problem. When the use triggers the extra content to appear, it is constructed using DOM methods. There may be a tiny delay, but the chances are that it will hardly be noticeable to the human eye.&lt;br /&gt;
&lt;br /&gt;
If the content generation may be slow (perhaps because it is waiting for an Ajax request) then you should display a progress icon. See, for example, the loading of the tooltip for help icons.&lt;br /&gt;
&lt;br /&gt;
===Don&#039;t break XHTML strict!===&lt;br /&gt;
&lt;br /&gt;
Remember that all Moodle output must be [[Development:XHTML|XHTML strict]], and that means that the HTML output must be well-formed XML. Inline JavaScript is a great way to break that. (JavaScript uses the &amp;lt; and &amp;amp; symbols that must be escaped in XML.) Therefore any JavaScript inline in the HTML should be escaped in a CDATA section:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code xml&amp;gt;&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
//&amp;lt;![CDATA[&lt;br /&gt;
&lt;br /&gt;
   // Your JavaScript code goes here.&lt;br /&gt;
&lt;br /&gt;
//]]&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course, if you are following the above guidelines and putting most of your JavaScript in separate .js files, and using $PAGE-&amp;gt;requires-&amp;gt;js_init_call, then this is taken care of for you automatically.&lt;br /&gt;
&lt;br /&gt;
==Testing==&lt;br /&gt;
&lt;br /&gt;
JavaScript support varies a lot between browsers. JavaScript needs to be tested in IE, Firefox and Safari. Ideally, Moodle will support [http://developer.yahoo.com/yui/articles/gbs/ all the browsers that YUI does].&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Development:Coding|The rest of Moodle coding guidelines]]&lt;br /&gt;
* [http://developer.yahoo.com/yui/ YUI documentation]&lt;br /&gt;
* [[Javascript FAQ]]&lt;br /&gt;
* [[Development:Unobtrusive Javascript]]&lt;br /&gt;
* [[Development:JavaScript functions]]&lt;br /&gt;
* [http://developer.yahoo.com/performance/rules.html Yahoo&#039;s Best Practices for Speeding Up Your Web Site]&lt;br /&gt;
&lt;br /&gt;
[[Category:Coding guidelines|JavaScript guidelines]]&lt;br /&gt;
[[Category:Javascript|JavaScript guidelines]]&lt;br /&gt;
[[Category:AJAX|JavaScript guidelines]]&lt;br /&gt;
&lt;br /&gt;
[[ja:開発:Javaスクリプトガイドライン]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Usuari:Alastair_Hole&amp;diff=74392</id>
		<title>Usuari:Alastair Hole</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Usuari:Alastair_Hole&amp;diff=74392"/>
		<updated>2010-08-01T17:42:07Z</updated>

		<summary type="html">&lt;p&gt;Afhole: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I&#039;m a Web Developer working for [http://www.wortech.ac.uk/ Worcester College of Technology], developing and supporting the college&#039;s Moodle implementation.&lt;br /&gt;
&lt;br /&gt;
I have also been involved in some plugin development, chiefly the [http://www.mrcute.co.uk/ MrCUTE repository]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Usuari:Alastair_Hole&amp;diff=74391</id>
		<title>Usuari:Alastair Hole</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Usuari:Alastair_Hole&amp;diff=74391"/>
		<updated>2010-08-01T17:41:26Z</updated>

		<summary type="html">&lt;p&gt;Afhole: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I&#039;m a Web Developer working for Worcester College of Technology, developing and supporting the college&#039;s Moodle implementation.&lt;br /&gt;
&lt;br /&gt;
I have also been involved in some plugin development, chiefly the [http://www.mrcute.co.uk MrCUTE repository]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Usuari:Alastair_Hole&amp;diff=74390</id>
		<title>Usuari:Alastair Hole</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Usuari:Alastair_Hole&amp;diff=74390"/>
		<updated>2010-08-01T17:41:12Z</updated>

		<summary type="html">&lt;p&gt;Afhole: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I&#039;m a Web Developer working for Worcester College of Technology, developing and supporting the college&#039;s Moodle implementation.&lt;br /&gt;
&lt;br /&gt;
I have also been involved in some plugin development, chiefly the [http://www.mrcute.co.uk MrCUTE repository MrCUTE]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Usuari:Alastair_Hole&amp;diff=74389</id>
		<title>Usuari:Alastair Hole</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Usuari:Alastair_Hole&amp;diff=74389"/>
		<updated>2010-08-01T17:38:46Z</updated>

		<summary type="html">&lt;p&gt;Afhole: New page: Author of MrCUTE Repository http://www.mrcute.co.uk&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Author of MrCUTE Repository http://www.mrcute.co.uk&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/id:9232&amp;diff=72940</id>
		<title>Broken/id:9232</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/id:9232&amp;diff=72940"/>
		<updated>2010-06-13T17:47:35Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Getting Moodle to load your JavaScript files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== event handlers ==&lt;br /&gt;
Should there be a recommendation to use YUI event handlers instead of JavaScript&#039;s inline event handlers? This might be better for cross-browser compatibility. --[[User:Frank Ralf|Frank Ralf]] 09:24, 15 June 2009 (UTC)&lt;br /&gt;
&lt;br /&gt;
:Good idea--[[User:Tim Hunt|Tim Hunt]] 03:16, 16 June 2009 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Getting Moodle to load your JavaScript files ==&lt;br /&gt;
are in_head and asap now obsolete?&lt;br /&gt;
It seems in_head is replaced by the second parameter to the js class constructor, is there an alternative for asap? --[[User:Alastair Hole|Alastair Hole]] 17:46, 13 June 2010 (UTC)&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/id:9232&amp;diff=72939</id>
		<title>Broken/id:9232</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/id:9232&amp;diff=72939"/>
		<updated>2010-06-13T17:46:57Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Getting Moodle to load your JavaScript files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== event handlers ==&lt;br /&gt;
Should there be a recommendation to use YUI event handlers instead of JavaScript&#039;s inline event handlers? This might be better for cross-browser compatibility. --[[User:Frank Ralf|Frank Ralf]] 09:24, 15 June 2009 (UTC)&lt;br /&gt;
&lt;br /&gt;
:Good idea--[[User:Tim Hunt|Tim Hunt]] 03:16, 16 June 2009 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Getting Moodle to load your JavaScript files ==&lt;br /&gt;
are in_head and asap now obsolete?&lt;br /&gt;
It seems in_head is replaced by the second parameter to the js class, is there an alternative for asap? --[[User:Alastair Hole|Alastair Hole]] 17:46, 13 June 2010 (UTC)&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/id:9232&amp;diff=72938</id>
		<title>Broken/id:9232</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/id:9232&amp;diff=72938"/>
		<updated>2010-06-13T17:46:17Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* event handlers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== event handlers ==&lt;br /&gt;
Should there be a recommendation to use YUI event handlers instead of JavaScript&#039;s inline event handlers? This might be better for cross-browser compatibility. --[[User:Frank Ralf|Frank Ralf]] 09:24, 15 June 2009 (UTC)&lt;br /&gt;
&lt;br /&gt;
:Good idea--[[User:Tim Hunt|Tim Hunt]] 03:16, 16 June 2009 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Getting Moodle to load your JavaScript files ==&lt;br /&gt;
are in_head and asap now obsolete?&lt;br /&gt;
It seems in_head is replaced by the second parameter to the js class, is there an alternative for asap?&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=File_upload_size&amp;diff=71456</id>
		<title>File upload size</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=File_upload_size&amp;diff=71456"/>
		<updated>2010-04-26T11:04:27Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Modifying the IIS 7.0/7.5 configuration (Windows Server 2008, Windows Server 2008 R2) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Probably the most frequently asked question in the Moodle.org Using Moodle forums is &amp;quot;How do I increase the upload file size limit?&amp;quot; The changes that need be made are the same in all versions of Moodle, just in different OS&#039; they need be made in different places. Upload file sizes are restricted in a number of ways and each one in this list restricts the following ones:&lt;br /&gt;
&lt;br /&gt;
 Server level&lt;br /&gt;
 Moodle site level&lt;br /&gt;
 Course level&lt;br /&gt;
 Activity level&lt;br /&gt;
&lt;br /&gt;
This is a contentious issue, mainly because you might think that it should be set inside the Moodle. Unfortunately, this is not so, these are environment issues that need to be set in the server and PHP folders, Moodle cannot work outside itself. &lt;br /&gt;
&lt;br /&gt;
==Physical access to Server==&lt;br /&gt;
These instructions assume you have full physical and administrative access to your server. If you are using a hosted server then you will probably need to look into other ways to increase your file upload size. &lt;br /&gt;
&lt;br /&gt;
There are positives and negatives to both methods below. If you modify the pnp.ini file then the changes will effect all php applications on your server. Since PHP5 you can only have one php.ini file on your server. The php.ini method will work with all web servers though. The .htaccess method will only effect the folder and all subfolders that it is placed in, but you must have certain settings enabled in Apache.&lt;br /&gt;
&lt;br /&gt;
===Modifying the php.ini file===&lt;br /&gt;
These instructions show you how to change the file upload size by editing your php.ini file.&lt;br /&gt;
====Ubuntu Linux Instructions====&lt;br /&gt;
&lt;br /&gt;
These instructions assume that you have installed the standard Moodle package, PHP 5 and Apache 2 via apt-get and left it all as a default install. If you have compiled yourself I presume that you will know where your php.ini files are!&lt;br /&gt;
&lt;br /&gt;
You need to edit the following three settings in your php.ini file located at: /etc/php5/apache2/&lt;br /&gt;
&lt;br /&gt;
*Type &amp;quot;sudo nano /etc/php5/apache2/php.ini&amp;quot;&lt;br /&gt;
*Press Ctrl and W and type &amp;quot;post_max_size&amp;quot; &lt;br /&gt;
*Change the value to the number of Mb you want your site to accept as uploads&lt;br /&gt;
*Press Ctrl and W and type &amp;quot;upload_max_filesize&amp;quot; &lt;br /&gt;
*Change the value to the number of Mb you want your site to accept as uploads&lt;br /&gt;
*Press Ctrl and W and type &amp;quot;max_execution_time&amp;quot; &lt;br /&gt;
*Change the value to 600&lt;br /&gt;
*Press Ctrl and O&lt;br /&gt;
*Press Ctrl and X&lt;br /&gt;
*Type sudo /etc/init.d/apache2 restart&lt;br /&gt;
&lt;br /&gt;
Your new file size limit should now appear in Administration &amp;gt; Security &amp;gt; Site Policies &amp;gt; Maximum uploaded file size&lt;br /&gt;
&lt;br /&gt;
====Windows XP and Server 2003 Instructions====&lt;br /&gt;
&lt;br /&gt;
These instructions presume that you have downloaded the latest PHP 5.2.x Windows zip package and extracted it to C:\PHP. If you have installed PHP to another location then change all references to &amp;quot;C:\PHP&amp;quot; to the location you installed PHP too.&lt;br /&gt;
&lt;br /&gt;
*Open C:\PHP&lt;br /&gt;
*Right Click the &#039;&#039;&#039;php.ini&#039;&#039;&#039; file in this folder and choose &amp;quot;Open with...&amp;quot;&lt;br /&gt;
*Choose &amp;quot;Wordpad&amp;quot; not &amp;quot;Notepad&amp;quot; to open the file with (Notepad does not properly use the UTF-8 Character set and it&#039;s carriage returns can cause problems)&lt;br /&gt;
**Better still download and install any text editor that can save the file in a UTF-8 format, [http://www.crimsoneditor.com Crimson Editor] is one such, and use that instead of either Wordpad or Notepad! &lt;br /&gt;
*Press Ctrl and F and type &amp;quot;post_max_size&amp;quot; &lt;br /&gt;
*Change the value to the number of Mb you want your site to accept as uploads&lt;br /&gt;
*Press Ctrl and F and type &amp;quot;upload_max_filesize&amp;quot; &lt;br /&gt;
*Change the value to the number of Mb you want your site to accept as uploads&lt;br /&gt;
*Press Ctrl and F and type &amp;quot;max_execution_time&amp;quot; &lt;br /&gt;
*Change the value to 600&lt;br /&gt;
*Press Ctrl and S&lt;br /&gt;
*Exit Wordpad&lt;br /&gt;
*Restart your webserver&lt;br /&gt;
**&#039;&#039;&#039;For IIS&#039;&#039;&#039;&lt;br /&gt;
**Open your Start Menu on your server and select &amp;quot;Run&amp;quot;&lt;br /&gt;
**Type &amp;quot;iisreset /RESTART&amp;quot;&lt;br /&gt;
**&#039;&#039;&#039;For Apache 2&#039;&#039;&#039;&lt;br /&gt;
**The following command will work as long as you have installed Apache 2 as a service on your Windows Server&lt;br /&gt;
**Open your Start Menu on your server and select &amp;quot;Run&amp;quot;&lt;br /&gt;
**Type &amp;quot;httpd -k restart&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Your new file size limit should now appear in Administration &amp;gt; Security &amp;gt; Site Policies &amp;gt; Maximum uploaded file size&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE:&#039;&#039;&#039; These instructions should also cover the Xampp Windows installer. Just replace C:\PHP with C:\Moodle\server\php and to restart your Moode with a normal stop-start.&lt;br /&gt;
&lt;br /&gt;
===Modifying the apache config file===&lt;br /&gt;
====Ubuntu Linux Instructions====&lt;br /&gt;
You may also need to edit the config.php file in the moodle directory:&lt;br /&gt;
*Type &amp;quot;gksudo nautilus&amp;quot; to get root permissions&lt;br /&gt;
*Navigate to /etc/moodle&lt;br /&gt;
*Open apache.conf&lt;br /&gt;
*Go to the &amp;quot;&amp;lt;IfModule mod_php5.c&amp;gt;&amp;quot; section&lt;br /&gt;
*Change &amp;quot;php_value upload_max_filesize = 2M&amp;quot; to a higher value&lt;br /&gt;
*Change &amp;quot;php_value post_max_size = 2M&amp;quot; to a higher value&lt;br /&gt;
*Go to the &amp;quot;&amp;lt;IfModule mod_php4.c&amp;gt;&amp;quot; section&lt;br /&gt;
*Change &amp;quot;php_value upload_max_filesize = 2M&amp;quot; to a higher value&lt;br /&gt;
*Change &amp;quot;php_value post_max_size = 2M&amp;quot; to a higher value&lt;br /&gt;
*Save file&lt;br /&gt;
*Type sudo /etc/init.d/apache2 restart&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modifying the .htaccess file===&lt;br /&gt;
{{stub}}&lt;br /&gt;
The following instructions will only work on an Apache web server, and also the Apache server must have Overrides allowed.  Additionally php must be running as an apache module, not as a cgi program.&lt;br /&gt;
&lt;br /&gt;
Create a file called .htaccess in Moodle&#039;s main directory (where &#039;index.php&#039; is located, not the &#039;moodledata&#039; directory) that contains the following information:&lt;br /&gt;
&lt;br /&gt;
 php_value upload_max_filesize 20971520&lt;br /&gt;
 php_value post_max_size 20971520&lt;br /&gt;
 php_value max_execution_time 600&lt;br /&gt;
&lt;br /&gt;
20971520 is the integer value for 20Mb. You can use the following site to [http://www.onlineconversion.com/computer_base2.htm convert MegaBytes to Bytes].&lt;br /&gt;
&lt;br /&gt;
===Modifying the IIS 7.0/7.5 configuration (Windows Server 2008, Windows Server 2008 R2)===&lt;br /&gt;
First increase activity and request time outs (allows large files to succeed on slow connections)&lt;br /&gt;
 FastCGI Settings &amp;gt; Edit (Right-click on PHP application)&lt;br /&gt;
 Set Process Model &amp;gt; Activity Timeout to &#039;3600&#039; (one hour)&lt;br /&gt;
 Set Process Model &amp;gt; Request Timeout to &#039;3600&#039; (one hour)&lt;br /&gt;
Next set &#039;Maximum allowed content length&#039;&lt;br /&gt;
 Request Filtering &amp;gt; Edit Feature Settings:&lt;br /&gt;
 Set &#039;Maximum allowed content length&#039; to your desired file size (in bytes) e.g. &#039;536870912&#039; for 512MB (default is approximately 28.6MB)&lt;br /&gt;
&lt;br /&gt;
==Hosted Server==&lt;br /&gt;
Things can be a little different with a hosted server for uploaded and downloaded file size.  You are probably going to  to be told to create or change a .htaccess file, or to modify a php.ini file.&lt;br /&gt;
&lt;br /&gt;
:It might be a good idea to talk to with your service provider before you attempt anything.  They probably have instructions on &amp;quot;how to&amp;quot; and may have their own limits for uploaded file size. Some hosts measure the file size in gigabytes and others in megabytes.  If you are unhappy with their limits, then check your contract and consider changing your provider to one that has a limit and price that you like.    &lt;br /&gt;
&lt;br /&gt;
===.htaccess with hosted server===&lt;br /&gt;
The one purpose of an .htaccess file is to override the the current limitations of both the server and the php.ini file.  Your hosted server should inform you where that file needs be placed in your Moodle, but generally in the root is sufficient. They may already have a standard file you can use, if so, use it - but perhaps not.  &lt;br /&gt;
&lt;br /&gt;
To the .htaccess file add the lines:&lt;br /&gt;
  php_value upload_max_filesize 128M&lt;br /&gt;
  php_value post_max_size 128M&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
This will limit uploads to 128MB, but you can make it any size you agree with your provider. The wording may vary slightly, according to the demands of the server.&lt;br /&gt;
&lt;br /&gt;
===php.ini with hosted server===&lt;br /&gt;
Some servers will not allow you to change the moodle root .htaccess file and tell you to use a php.ini file for php directives.  Here you can use the instruction located in the section above called [[File_upload_size#Modifying_the_php.ini_file|Modifying the php.ini file]].&lt;br /&gt;
&lt;br /&gt;
Find the php.ini file in your moodle subfolder on your hosted server. You might want to copy the file as a backup just in case.  Edit php.ini, find &amp;quot;upload_max_filesize&amp;quot; and post_max_size in the code.  After the = change the number.  Here the max filesize is 20 megabytes.  &lt;br /&gt;
&lt;br /&gt;
 upload_max_filesize = 20M&lt;br /&gt;
 post_max_size = 20M&lt;br /&gt;
&lt;br /&gt;
:Tip: Still not changed?  Some hosts using cpanel have a php config program under services/software.   Use the &amp;quot;Single php.ini&amp;quot; option and make sure you note the location of the php.ini file to modify.  This changes the .htaccess file in the same area and thus the server limit for all programs using php.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[[Administration_FAQ#How_do_the_limits_on_uploaded_files_work.3F|Administration FAQ Doc page]]&lt;br /&gt;
*[[Site_policies#Maximum_uploaded_file_size|Site Policies Doc page]]&lt;br /&gt;
*[[Installing_Moodle/Creating_custom_php.ini_files|Creating custom php.ini files Doc Page]]&lt;br /&gt;
* Using Moodle [http://moodle.org/mod/forum/discuss.php?d=39625 Detailed instructions to increase the maximum allowed size for uploaded files] forum discussion&lt;br /&gt;
* Using Moodle [http://moodle.org/mod/forum/discuss.php?d=97907 Instructions to increase maximum allowed size on hosted servers] forum discussion&lt;br /&gt;
* Using Moodle [http://moodle.org/mod/forum/discuss.php?d=124441 Help on changing the maximum upload size when installing Moodle via apt-get] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Administrator|File]][[Category:FAQ|File]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=File_upload_size&amp;diff=71455</id>
		<title>File upload size</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=File_upload_size&amp;diff=71455"/>
		<updated>2010-04-26T11:04:03Z</updated>

		<summary type="html">&lt;p&gt;Afhole: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Probably the most frequently asked question in the Moodle.org Using Moodle forums is &amp;quot;How do I increase the upload file size limit?&amp;quot; The changes that need be made are the same in all versions of Moodle, just in different OS&#039; they need be made in different places. Upload file sizes are restricted in a number of ways and each one in this list restricts the following ones:&lt;br /&gt;
&lt;br /&gt;
 Server level&lt;br /&gt;
 Moodle site level&lt;br /&gt;
 Course level&lt;br /&gt;
 Activity level&lt;br /&gt;
&lt;br /&gt;
This is a contentious issue, mainly because you might think that it should be set inside the Moodle. Unfortunately, this is not so, these are environment issues that need to be set in the server and PHP folders, Moodle cannot work outside itself. &lt;br /&gt;
&lt;br /&gt;
==Physical access to Server==&lt;br /&gt;
These instructions assume you have full physical and administrative access to your server. If you are using a hosted server then you will probably need to look into other ways to increase your file upload size. &lt;br /&gt;
&lt;br /&gt;
There are positives and negatives to both methods below. If you modify the pnp.ini file then the changes will effect all php applications on your server. Since PHP5 you can only have one php.ini file on your server. The php.ini method will work with all web servers though. The .htaccess method will only effect the folder and all subfolders that it is placed in, but you must have certain settings enabled in Apache.&lt;br /&gt;
&lt;br /&gt;
===Modifying the php.ini file===&lt;br /&gt;
These instructions show you how to change the file upload size by editing your php.ini file.&lt;br /&gt;
====Ubuntu Linux Instructions====&lt;br /&gt;
&lt;br /&gt;
These instructions assume that you have installed the standard Moodle package, PHP 5 and Apache 2 via apt-get and left it all as a default install. If you have compiled yourself I presume that you will know where your php.ini files are!&lt;br /&gt;
&lt;br /&gt;
You need to edit the following three settings in your php.ini file located at: /etc/php5/apache2/&lt;br /&gt;
&lt;br /&gt;
*Type &amp;quot;sudo nano /etc/php5/apache2/php.ini&amp;quot;&lt;br /&gt;
*Press Ctrl and W and type &amp;quot;post_max_size&amp;quot; &lt;br /&gt;
*Change the value to the number of Mb you want your site to accept as uploads&lt;br /&gt;
*Press Ctrl and W and type &amp;quot;upload_max_filesize&amp;quot; &lt;br /&gt;
*Change the value to the number of Mb you want your site to accept as uploads&lt;br /&gt;
*Press Ctrl and W and type &amp;quot;max_execution_time&amp;quot; &lt;br /&gt;
*Change the value to 600&lt;br /&gt;
*Press Ctrl and O&lt;br /&gt;
*Press Ctrl and X&lt;br /&gt;
*Type sudo /etc/init.d/apache2 restart&lt;br /&gt;
&lt;br /&gt;
Your new file size limit should now appear in Administration &amp;gt; Security &amp;gt; Site Policies &amp;gt; Maximum uploaded file size&lt;br /&gt;
&lt;br /&gt;
====Windows XP and Server 2003 Instructions====&lt;br /&gt;
&lt;br /&gt;
These instructions presume that you have downloaded the latest PHP 5.2.x Windows zip package and extracted it to C:\PHP. If you have installed PHP to another location then change all references to &amp;quot;C:\PHP&amp;quot; to the location you installed PHP too.&lt;br /&gt;
&lt;br /&gt;
*Open C:\PHP&lt;br /&gt;
*Right Click the &#039;&#039;&#039;php.ini&#039;&#039;&#039; file in this folder and choose &amp;quot;Open with...&amp;quot;&lt;br /&gt;
*Choose &amp;quot;Wordpad&amp;quot; not &amp;quot;Notepad&amp;quot; to open the file with (Notepad does not properly use the UTF-8 Character set and it&#039;s carriage returns can cause problems)&lt;br /&gt;
**Better still download and install any text editor that can save the file in a UTF-8 format, [http://www.crimsoneditor.com Crimson Editor] is one such, and use that instead of either Wordpad or Notepad! &lt;br /&gt;
*Press Ctrl and F and type &amp;quot;post_max_size&amp;quot; &lt;br /&gt;
*Change the value to the number of Mb you want your site to accept as uploads&lt;br /&gt;
*Press Ctrl and F and type &amp;quot;upload_max_filesize&amp;quot; &lt;br /&gt;
*Change the value to the number of Mb you want your site to accept as uploads&lt;br /&gt;
*Press Ctrl and F and type &amp;quot;max_execution_time&amp;quot; &lt;br /&gt;
*Change the value to 600&lt;br /&gt;
*Press Ctrl and S&lt;br /&gt;
*Exit Wordpad&lt;br /&gt;
*Restart your webserver&lt;br /&gt;
**&#039;&#039;&#039;For IIS&#039;&#039;&#039;&lt;br /&gt;
**Open your Start Menu on your server and select &amp;quot;Run&amp;quot;&lt;br /&gt;
**Type &amp;quot;iisreset /RESTART&amp;quot;&lt;br /&gt;
**&#039;&#039;&#039;For Apache 2&#039;&#039;&#039;&lt;br /&gt;
**The following command will work as long as you have installed Apache 2 as a service on your Windows Server&lt;br /&gt;
**Open your Start Menu on your server and select &amp;quot;Run&amp;quot;&lt;br /&gt;
**Type &amp;quot;httpd -k restart&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Your new file size limit should now appear in Administration &amp;gt; Security &amp;gt; Site Policies &amp;gt; Maximum uploaded file size&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE:&#039;&#039;&#039; These instructions should also cover the Xampp Windows installer. Just replace C:\PHP with C:\Moodle\server\php and to restart your Moode with a normal stop-start.&lt;br /&gt;
&lt;br /&gt;
===Modifying the apache config file===&lt;br /&gt;
====Ubuntu Linux Instructions====&lt;br /&gt;
You may also need to edit the config.php file in the moodle directory:&lt;br /&gt;
*Type &amp;quot;gksudo nautilus&amp;quot; to get root permissions&lt;br /&gt;
*Navigate to /etc/moodle&lt;br /&gt;
*Open apache.conf&lt;br /&gt;
*Go to the &amp;quot;&amp;lt;IfModule mod_php5.c&amp;gt;&amp;quot; section&lt;br /&gt;
*Change &amp;quot;php_value upload_max_filesize = 2M&amp;quot; to a higher value&lt;br /&gt;
*Change &amp;quot;php_value post_max_size = 2M&amp;quot; to a higher value&lt;br /&gt;
*Go to the &amp;quot;&amp;lt;IfModule mod_php4.c&amp;gt;&amp;quot; section&lt;br /&gt;
*Change &amp;quot;php_value upload_max_filesize = 2M&amp;quot; to a higher value&lt;br /&gt;
*Change &amp;quot;php_value post_max_size = 2M&amp;quot; to a higher value&lt;br /&gt;
*Save file&lt;br /&gt;
*Type sudo /etc/init.d/apache2 restart&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modifying the .htaccess file===&lt;br /&gt;
{{stub}}&lt;br /&gt;
The following instructions will only work on an Apache web server, and also the Apache server must have Overrides allowed.  Additionally php must be running as an apache module, not as a cgi program.&lt;br /&gt;
&lt;br /&gt;
Create a file called .htaccess in Moodle&#039;s main directory (where &#039;index.php&#039; is located, not the &#039;moodledata&#039; directory) that contains the following information:&lt;br /&gt;
&lt;br /&gt;
 php_value upload_max_filesize 20971520&lt;br /&gt;
 php_value post_max_size 20971520&lt;br /&gt;
 php_value max_execution_time 600&lt;br /&gt;
&lt;br /&gt;
20971520 is the integer value for 20Mb. You can use the following site to [http://www.onlineconversion.com/computer_base2.htm convert MegaBytes to Bytes].&lt;br /&gt;
&lt;br /&gt;
===Modifying the IIS 7.0/7.5 configuration (Windows Server 2008, Windows Server 2008 R2)===&lt;br /&gt;
First increase activity and request time outs (allows large files to succeed on slow connections)&lt;br /&gt;
 FastCGI Settings &amp;gt; Edit (Right-click on PHP application&lt;br /&gt;
 Set Process Model &amp;gt; Activity Timeout to &#039;3600&#039; (one hour)&lt;br /&gt;
 Set Process Model &amp;gt; Request Timeout to &#039;3600&#039; (one hour)&lt;br /&gt;
Next set &#039;Maximum allowed content length&#039;&lt;br /&gt;
 Request Filtering &amp;gt; Edit Feature Settings:&lt;br /&gt;
 Set &#039;Maximum allowed content length&#039; to your desired file size (in bytes) e.g. &#039;536870912&#039; for 512MB (default is approximately 28.6MB)&lt;br /&gt;
&lt;br /&gt;
==Hosted Server==&lt;br /&gt;
Things can be a little different with a hosted server for uploaded and downloaded file size.  You are probably going to  to be told to create or change a .htaccess file, or to modify a php.ini file.&lt;br /&gt;
&lt;br /&gt;
:It might be a good idea to talk to with your service provider before you attempt anything.  They probably have instructions on &amp;quot;how to&amp;quot; and may have their own limits for uploaded file size. Some hosts measure the file size in gigabytes and others in megabytes.  If you are unhappy with their limits, then check your contract and consider changing your provider to one that has a limit and price that you like.    &lt;br /&gt;
&lt;br /&gt;
===.htaccess with hosted server===&lt;br /&gt;
The one purpose of an .htaccess file is to override the the current limitations of both the server and the php.ini file.  Your hosted server should inform you where that file needs be placed in your Moodle, but generally in the root is sufficient. They may already have a standard file you can use, if so, use it - but perhaps not.  &lt;br /&gt;
&lt;br /&gt;
To the .htaccess file add the lines:&lt;br /&gt;
  php_value upload_max_filesize 128M&lt;br /&gt;
  php_value post_max_size 128M&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
This will limit uploads to 128MB, but you can make it any size you agree with your provider. The wording may vary slightly, according to the demands of the server.&lt;br /&gt;
&lt;br /&gt;
===php.ini with hosted server===&lt;br /&gt;
Some servers will not allow you to change the moodle root .htaccess file and tell you to use a php.ini file for php directives.  Here you can use the instruction located in the section above called [[File_upload_size#Modifying_the_php.ini_file|Modifying the php.ini file]].&lt;br /&gt;
&lt;br /&gt;
Find the php.ini file in your moodle subfolder on your hosted server. You might want to copy the file as a backup just in case.  Edit php.ini, find &amp;quot;upload_max_filesize&amp;quot; and post_max_size in the code.  After the = change the number.  Here the max filesize is 20 megabytes.  &lt;br /&gt;
&lt;br /&gt;
 upload_max_filesize = 20M&lt;br /&gt;
 post_max_size = 20M&lt;br /&gt;
&lt;br /&gt;
:Tip: Still not changed?  Some hosts using cpanel have a php config program under services/software.   Use the &amp;quot;Single php.ini&amp;quot; option and make sure you note the location of the php.ini file to modify.  This changes the .htaccess file in the same area and thus the server limit for all programs using php.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[[Administration_FAQ#How_do_the_limits_on_uploaded_files_work.3F|Administration FAQ Doc page]]&lt;br /&gt;
*[[Site_policies#Maximum_uploaded_file_size|Site Policies Doc page]]&lt;br /&gt;
*[[Installing_Moodle/Creating_custom_php.ini_files|Creating custom php.ini files Doc Page]]&lt;br /&gt;
* Using Moodle [http://moodle.org/mod/forum/discuss.php?d=39625 Detailed instructions to increase the maximum allowed size for uploaded files] forum discussion&lt;br /&gt;
* Using Moodle [http://moodle.org/mod/forum/discuss.php?d=97907 Instructions to increase maximum allowed size on hosted servers] forum discussion&lt;br /&gt;
* Using Moodle [http://moodle.org/mod/forum/discuss.php?d=124441 Help on changing the maximum upload size when installing Moodle via apt-get] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Administrator|File]][[Category:FAQ|File]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=File_upload_size&amp;diff=71454</id>
		<title>File upload size</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=File_upload_size&amp;diff=71454"/>
		<updated>2010-04-26T11:03:06Z</updated>

		<summary type="html">&lt;p&gt;Afhole: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Probably the most frequently asked question in the Moodle.org Using Moodle forums is &amp;quot;How do I increase the upload file size limit?&amp;quot; The changes that need be made are the same in all versions of Moodle, just in different OS&#039; they need be made in different places. Upload file sizes are restricted in a number of ways and each one in this list restricts the following ones:&lt;br /&gt;
&lt;br /&gt;
 Server level&lt;br /&gt;
 Moodle site level&lt;br /&gt;
 Course level&lt;br /&gt;
 Activity level&lt;br /&gt;
&lt;br /&gt;
This is a contentious issue, mainly because you might think that it should be set inside the Moodle. Unfortunately, this is not so, these are environment issues that need to be set in the server and PHP folders, Moodle cannot work outside itself. &lt;br /&gt;
&lt;br /&gt;
==Physical access to Server==&lt;br /&gt;
These instructions assume you have full physical and administrative access to your server. If you are using a hosted server then you will probably need to look into other ways to increase your file upload size. &lt;br /&gt;
&lt;br /&gt;
There are positives and negatives to both methods below. If you modify the pnp.ini file then the changes will effect all php applications on your server. Since PHP5 you can only have one php.ini file on your server. The php.ini method will work with all web servers though. The .htaccess method will only effect the folder and all subfolders that it is placed in, but you must have certain settings enabled in Apache.&lt;br /&gt;
&lt;br /&gt;
===Modifying the php.ini file===&lt;br /&gt;
These instructions show you how to change the file upload size by editing your php.ini file.&lt;br /&gt;
====Ubuntu Linux Instructions====&lt;br /&gt;
&lt;br /&gt;
These instructions assume that you have installed the standard Moodle package, PHP 5 and Apache 2 via apt-get and left it all as a default install. If you have compiled yourself I presume that you will know where your php.ini files are!&lt;br /&gt;
&lt;br /&gt;
You need to edit the following three settings in your php.ini file located at: /etc/php5/apache2/&lt;br /&gt;
&lt;br /&gt;
*Type &amp;quot;sudo nano /etc/php5/apache2/php.ini&amp;quot;&lt;br /&gt;
*Press Ctrl and W and type &amp;quot;post_max_size&amp;quot; &lt;br /&gt;
*Change the value to the number of Mb you want your site to accept as uploads&lt;br /&gt;
*Press Ctrl and W and type &amp;quot;upload_max_filesize&amp;quot; &lt;br /&gt;
*Change the value to the number of Mb you want your site to accept as uploads&lt;br /&gt;
*Press Ctrl and W and type &amp;quot;max_execution_time&amp;quot; &lt;br /&gt;
*Change the value to 600&lt;br /&gt;
*Press Ctrl and O&lt;br /&gt;
*Press Ctrl and X&lt;br /&gt;
*Type sudo /etc/init.d/apache2 restart&lt;br /&gt;
&lt;br /&gt;
Your new file size limit should now appear in Administration &amp;gt; Security &amp;gt; Site Policies &amp;gt; Maximum uploaded file size&lt;br /&gt;
&lt;br /&gt;
====Windows XP and Server 2003 Instructions====&lt;br /&gt;
&lt;br /&gt;
These instructions presume that you have downloaded the latest PHP 5.2.x Windows zip package and extracted it to C:\PHP. If you have installed PHP to another location then change all references to &amp;quot;C:\PHP&amp;quot; to the location you installed PHP too.&lt;br /&gt;
&lt;br /&gt;
*Open C:\PHP&lt;br /&gt;
*Right Click the &#039;&#039;&#039;php.ini&#039;&#039;&#039; file in this folder and choose &amp;quot;Open with...&amp;quot;&lt;br /&gt;
*Choose &amp;quot;Wordpad&amp;quot; not &amp;quot;Notepad&amp;quot; to open the file with (Notepad does not properly use the UTF-8 Character set and it&#039;s carriage returns can cause problems)&lt;br /&gt;
**Better still download and install any text editor that can save the file in a UTF-8 format, [http://www.crimsoneditor.com Crimson Editor] is one such, and use that instead of either Wordpad or Notepad! &lt;br /&gt;
*Press Ctrl and F and type &amp;quot;post_max_size&amp;quot; &lt;br /&gt;
*Change the value to the number of Mb you want your site to accept as uploads&lt;br /&gt;
*Press Ctrl and F and type &amp;quot;upload_max_filesize&amp;quot; &lt;br /&gt;
*Change the value to the number of Mb you want your site to accept as uploads&lt;br /&gt;
*Press Ctrl and F and type &amp;quot;max_execution_time&amp;quot; &lt;br /&gt;
*Change the value to 600&lt;br /&gt;
*Press Ctrl and S&lt;br /&gt;
*Exit Wordpad&lt;br /&gt;
*Restart your webserver&lt;br /&gt;
**&#039;&#039;&#039;For IIS&#039;&#039;&#039;&lt;br /&gt;
**Open your Start Menu on your server and select &amp;quot;Run&amp;quot;&lt;br /&gt;
**Type &amp;quot;iisreset /RESTART&amp;quot;&lt;br /&gt;
**&#039;&#039;&#039;For Apache 2&#039;&#039;&#039;&lt;br /&gt;
**The following command will work as long as you have installed Apache 2 as a service on your Windows Server&lt;br /&gt;
**Open your Start Menu on your server and select &amp;quot;Run&amp;quot;&lt;br /&gt;
**Type &amp;quot;httpd -k restart&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Your new file size limit should now appear in Administration &amp;gt; Security &amp;gt; Site Policies &amp;gt; Maximum uploaded file size&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE:&#039;&#039;&#039; These instructions should also cover the Xampp Windows installer. Just replace C:\PHP with C:\Moodle\server\php and to restart your Moode with a normal stop-start.&lt;br /&gt;
&lt;br /&gt;
===Modifying the apache config file===&lt;br /&gt;
====Ubuntu Linux Instructions====&lt;br /&gt;
You may also need to edit the config.php file in the moodle directory:&lt;br /&gt;
*Type &amp;quot;gksudo nautilus&amp;quot; to get root permissions&lt;br /&gt;
*Navigate to /etc/moodle&lt;br /&gt;
*Open apache.conf&lt;br /&gt;
*Go to the &amp;quot;&amp;lt;IfModule mod_php5.c&amp;gt;&amp;quot; section&lt;br /&gt;
*Change &amp;quot;php_value upload_max_filesize = 2M&amp;quot; to a higher value&lt;br /&gt;
*Change &amp;quot;php_value post_max_size = 2M&amp;quot; to a higher value&lt;br /&gt;
*Go to the &amp;quot;&amp;lt;IfModule mod_php4.c&amp;gt;&amp;quot; section&lt;br /&gt;
*Change &amp;quot;php_value upload_max_filesize = 2M&amp;quot; to a higher value&lt;br /&gt;
*Change &amp;quot;php_value post_max_size = 2M&amp;quot; to a higher value&lt;br /&gt;
*Save file&lt;br /&gt;
*Type sudo /etc/init.d/apache2 restart&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Modifying the .htaccess file===&lt;br /&gt;
{{stub}}&lt;br /&gt;
The following instructions will only work on an Apache web server, and also the Apache server must have Overrides allowed.  Additionally php must be running as an apache module, not as a cgi program.&lt;br /&gt;
&lt;br /&gt;
Create a file called .htaccess in Moodle&#039;s main directory (where &#039;index.php&#039; is located, not the &#039;moodledata&#039; directory) that contains the following information:&lt;br /&gt;
&lt;br /&gt;
 php_value upload_max_filesize 20971520&lt;br /&gt;
 php_value post_max_size 20971520&lt;br /&gt;
 php_value max_execution_time 600&lt;br /&gt;
&lt;br /&gt;
20971520 is the integer value for 20Mb. You can use the following site to [http://www.onlineconversion.com/computer_base2.htm convert MegaBytes to Bytes].&lt;br /&gt;
&lt;br /&gt;
===Modifying the IIS 7.0/7.5 configuration (Windows Server 2008 SP2 &amp;amp; R2)===&lt;br /&gt;
First increase activity and request time outs (allows large files to succeed on slow connections)&lt;br /&gt;
 FastCGI Settings &amp;gt; Edit (Right-click on PHP application&lt;br /&gt;
 Set Process Model &amp;gt; Activity Timeout to &#039;3600&#039; (one hour)&lt;br /&gt;
 Set Process Model &amp;gt; Request Timeout to &#039;3600&#039; (one hour)&lt;br /&gt;
Next set &#039;Maximum allowed content length&#039;&lt;br /&gt;
 Request Filtering &amp;gt; Edit Feature Settings:&lt;br /&gt;
 Set &#039;Maximum allowed content length&#039; to your desired file size (in bytes) e.g. &#039;536870912&#039; for 512MB (default is approximately 28.6MB)&lt;br /&gt;
&lt;br /&gt;
==Hosted Server==&lt;br /&gt;
Things can be a little different with a hosted server for uploaded and downloaded file size.  You are probably going to  to be told to create or change a .htaccess file, or to modify a php.ini file.&lt;br /&gt;
&lt;br /&gt;
:It might be a good idea to talk to with your service provider before you attempt anything.  They probably have instructions on &amp;quot;how to&amp;quot; and may have their own limits for uploaded file size. Some hosts measure the file size in gigabytes and others in megabytes.  If you are unhappy with their limits, then check your contract and consider changing your provider to one that has a limit and price that you like.    &lt;br /&gt;
&lt;br /&gt;
===.htaccess with hosted server===&lt;br /&gt;
The one purpose of an .htaccess file is to override the the current limitations of both the server and the php.ini file.  Your hosted server should inform you where that file needs be placed in your Moodle, but generally in the root is sufficient. They may already have a standard file you can use, if so, use it - but perhaps not.  &lt;br /&gt;
&lt;br /&gt;
To the .htaccess file add the lines:&lt;br /&gt;
  php_value upload_max_filesize 128M&lt;br /&gt;
  php_value post_max_size 128M&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
This will limit uploads to 128MB, but you can make it any size you agree with your provider. The wording may vary slightly, according to the demands of the server.&lt;br /&gt;
&lt;br /&gt;
===php.ini with hosted server===&lt;br /&gt;
Some servers will not allow you to change the moodle root .htaccess file and tell you to use a php.ini file for php directives.  Here you can use the instruction located in the section above called [[File_upload_size#Modifying_the_php.ini_file|Modifying the php.ini file]].&lt;br /&gt;
&lt;br /&gt;
Find the php.ini file in your moodle subfolder on your hosted server. You might want to copy the file as a backup just in case.  Edit php.ini, find &amp;quot;upload_max_filesize&amp;quot; and post_max_size in the code.  After the = change the number.  Here the max filesize is 20 megabytes.  &lt;br /&gt;
&lt;br /&gt;
 upload_max_filesize = 20M&lt;br /&gt;
 post_max_size = 20M&lt;br /&gt;
&lt;br /&gt;
:Tip: Still not changed?  Some hosts using cpanel have a php config program under services/software.   Use the &amp;quot;Single php.ini&amp;quot; option and make sure you note the location of the php.ini file to modify.  This changes the .htaccess file in the same area and thus the server limit for all programs using php.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
*[[Administration_FAQ#How_do_the_limits_on_uploaded_files_work.3F|Administration FAQ Doc page]]&lt;br /&gt;
*[[Site_policies#Maximum_uploaded_file_size|Site Policies Doc page]]&lt;br /&gt;
*[[Installing_Moodle/Creating_custom_php.ini_files|Creating custom php.ini files Doc Page]]&lt;br /&gt;
* Using Moodle [http://moodle.org/mod/forum/discuss.php?d=39625 Detailed instructions to increase the maximum allowed size for uploaded files] forum discussion&lt;br /&gt;
* Using Moodle [http://moodle.org/mod/forum/discuss.php?d=97907 Instructions to increase maximum allowed size on hosted servers] forum discussion&lt;br /&gt;
* Using Moodle [http://moodle.org/mod/forum/discuss.php?d=124441 Help on changing the maximum upload size when installing Moodle via apt-get] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Administrator|File]][[Category:FAQ|File]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=MrCUTE&amp;diff=66360</id>
		<title>MrCUTE</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=MrCUTE&amp;diff=66360"/>
		<updated>2009-12-08T14:43:09Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* NLN (Noodle) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;MrCute is contributed code and a JISC funded project that extends the functionality of the IMS Repository system (Object module) originally developed by Alton College, UK. It adds the following main features:&lt;br /&gt;
* Searchable database index of all IMS Packages (backwards compatible with resources deployed through the current IMS Repository system)&lt;br /&gt;
* Editing of existing IMS Content Packages&lt;br /&gt;
* Create new packages from single files uploaded to Moodle, e.g. Images, Documents, Videos, Flash movies&lt;br /&gt;
&lt;br /&gt;
[[Category: Contributed code]]&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
&lt;br /&gt;
Please refer to the readme file in the zip package:&lt;br /&gt;
[zip]:/blocks/mrcute/docs/README.txt&lt;br /&gt;
&lt;br /&gt;
== NLN (Noodle) ==&lt;br /&gt;
To enable NLN/Noodle integration you must install Noodle, available from:&lt;br /&gt;
[http://www.nln.ac.uk/?p=Noodle Noodle]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=MrCUTE&amp;diff=66359</id>
		<title>MrCUTE</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=MrCUTE&amp;diff=66359"/>
		<updated>2009-12-08T14:42:46Z</updated>

		<summary type="html">&lt;p&gt;Afhole: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;MrCute is contributed code and a JISC funded project that extends the functionality of the IMS Repository system (Object module) originally developed by Alton College, UK. It adds the following main features:&lt;br /&gt;
* Searchable database index of all IMS Packages (backwards compatible with resources deployed through the current IMS Repository system)&lt;br /&gt;
* Editing of existing IMS Content Packages&lt;br /&gt;
* Create new packages from single files uploaded to Moodle, e.g. Images, Documents, Videos, Flash movies&lt;br /&gt;
&lt;br /&gt;
[[Category: Contributed code]]&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
&lt;br /&gt;
Please refer to the readme file in the zip package:&lt;br /&gt;
[zip]:/blocks/mrcute/docs/README.txt&lt;br /&gt;
&lt;br /&gt;
== NLN (Noodle) ==&lt;br /&gt;
To enable NLN/Noodle integration you must install Noodle, available from:&lt;br /&gt;
[http://www.nln.ac.uk/?p=Noodle Download Noodle]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=MrCUTE&amp;diff=65185</id>
		<title>MrCUTE</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=MrCUTE&amp;diff=65185"/>
		<updated>2009-11-13T15:44:47Z</updated>

		<summary type="html">&lt;p&gt;Afhole: New page: MrCute is a JISC funded project that extends the functionality of the IMS Repository system (Object module) originally developed by Alton College, UK. It adds the following main features: ...&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;MrCute is a JISC funded project that extends the functionality of the IMS Repository system (Object module) originally developed by Alton College, UK. It adds the following main features: &lt;br /&gt;
Searchable database index of all IMS Packages (backwards compatible with resources deployed through the current IMS Repository system)&lt;br /&gt;
Editing of existing IMS Content Packages&lt;br /&gt;
Create new packages from single files uploaded to Moodle, e.g. Images, Documents, Videos, Flash movies&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Installing_MSSQL_for_PHP&amp;diff=63157</id>
		<title>Installing MSSQL for PHP</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Installing_MSSQL_for_PHP&amp;diff=63157"/>
		<updated>2009-09-17T15:43:02Z</updated>

		<summary type="html">&lt;p&gt;Afhole: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.7}}&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This short manual is suitable if you are trying to run Moodle 1.7 (and upwards) using the SQL*Server (MSSQL) RDBMS. Steps detailed below must be performed &#039;&#039;&#039;before&#039;&#039;&#039; installing Moodle itself.&lt;br /&gt;
&lt;br /&gt;
First of all, minimum required version of MSSQL has been stabilised to MSSQL 2005 (v.9), although it &#039;&#039;&#039;might work with MSSQL 2000 (v.8) or newer&#039;&#039;&#039;. All the development process has been performed using MSSQL 2005 and there could be some &#039;&#039;&#039;unknown problems&#039;&#039;&#039; with previous releases.&lt;br /&gt;
&lt;br /&gt;
While PHP comes with one, more or less, standard extension (mssql) that provides access to MSSQL databases, early we found some hard limits on it. Basically such default extension has some limits that prevent us to use it at all (you can find more info about these problems [[Development:XMLDB problems#MSSQL, PHP, UTF-8 and UCS-2|here]]).&lt;br /&gt;
&lt;br /&gt;
So, in order to allow PHP (i.e. Moodle) to access to MSSQL DBs properly we have to install a &#039;&#039;&#039;mssql extension alternative&#039;&#039;&#039; to save us from the problems related above. See the sections below for details about the various options.&lt;br /&gt;
&lt;br /&gt;
== Installation overview ==&lt;br /&gt;
&lt;br /&gt;
1. Get MSSQL Server installed and running. ([http://www.microsoft.com/sql/editions/express/default.mspx A free limited version, SQL Server Express Edition] is available for testing.)&lt;br /&gt;
:Make sure that you choose mixed authentication (Windows and local accounts) to keep things simpler later.  You&#039;ll be asked to define the  &amp;quot;sa&amp;quot; account password (it&#039;s the default System Administrator account which has full access to all databases by default).&lt;br /&gt;
&lt;br /&gt;
2. Make sure MS SQL Server can accept incoming TCP/IP connections on port 1433 (the standard one).&lt;br /&gt;
:You might need to explicitly allow this in your Windows firewall (see the Control Panel).  You may also need to edit options in the :&#039;&#039;&#039;SQL Server Configuration Manager&#039;&#039;&#039; -&amp;gt; &#039;&#039;&#039;Network Configuration&#039;&#039;&#039; -&amp;gt; &#039;&#039;&#039;Protocols&#039;&#039;&#039; -&amp;gt; &#039;&#039;&#039;TCP/IP enabled&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
3. Open the &amp;quot;SQL Server Management Studio&amp;quot; and create a new empty database.  If you are using the &amp;quot;sa&amp;quot; account then you don&#039;t need to do anything else here.&lt;br /&gt;
&lt;br /&gt;
4. Configure these settings in your created (and still empty) database:&lt;br /&gt;
&lt;br /&gt;
:* ANSI NULLS Enabled = true (ALTER DATABASE mdl_HEAD SET ANSI_NULLS ON GO)&lt;br /&gt;
:* Quoted Identifiers Enabled = true (ALTER DATABASE mdl_HEAD SET QUOTED_IDENTIFIER ON GO)&lt;br /&gt;
&lt;br /&gt;
5. Get PHP installed with a web server.   Unless you want to do it under IIS or some other way, the packages on the [http://download.moodle.org Moodle download page] are a good solution.&lt;br /&gt;
&lt;br /&gt;
6. Choose one of the following specific sections for your server to install the &#039;&#039;&#039;mssql extension alternative&#039;&#039;&#039; installed and running properly on your PHP box.&lt;br /&gt;
&lt;br /&gt;
7. Set the following settings in your php.ini file&lt;br /&gt;
:* mssql.textlimit = 20971520&lt;br /&gt;
:* mssql.textsize = 20971520&lt;br /&gt;
:Also, don&#039;t forget to set one of the following &#039;&#039;&#039;alternatives&#039;&#039;&#039;, in order to get all the data properly &amp;quot;slashed&amp;quot;:&lt;br /&gt;
:* magic_quotes_gpc = Off  &#039;&#039;&#039;or&#039;&#039;&#039;&lt;br /&gt;
:* magic_quotes_gpc = On &#039;&#039;&#039;and&#039;&#039;&#039; magic_quotes_sybase = On &lt;br /&gt;
&lt;br /&gt;
8. With all this properly configured, you can continue with a [[Installing Moodle|standard Moodle installation]].&lt;br /&gt;
&lt;br /&gt;
== Using FreeTDS on Unix ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p class=&amp;quot;note&amp;quot;&amp;gt;&#039;&#039;&#039;Important Note 1:&#039;&#039;&#039; Due to [http://bugs.php.net/bug.php?id=39213 one bug in PHP] it&#039;s highly recommendable to use PHP &amp;gt; 5.1.6 with FreeTDS ([http://tracker.moodle.org/browse/MDL-11810 more info]).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p class=&amp;quot;note&amp;quot;&amp;gt;&#039;&#039;&#039;Important Note 2:&#039;&#039;&#039; Due to one bug in how FreeTDS handles nulls and empty values for some text types it&#039;s highly recommendable to use a recent version of FreeTDS (0.64 + official patches) ([http://tracker.moodle.org/browse/MDL-11810#action_38005 more info]).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you web server is on Linux or some other flavour of Unix, try FreeTDS, http://www.freetds.org (documentation at http://www.freetds.org/docs.html)&lt;br /&gt;
&lt;br /&gt;
Note that the download link above is a &#039;&#039;&#039;source download&#039;&#039;&#039;, so you will need to install and compile it properly.&lt;br /&gt;
&lt;br /&gt;
Once downloaded and uncompressed you must &#039;&#039;&#039;&amp;quot;configure, make, make install&amp;quot;&#039;&#039;&#039; it. This will deploy some stuff in the &amp;quot;/usr/local&amp;quot; directory of your machine, mainly:&lt;br /&gt;
* /usr/local/etc: where the freetds conf files will reside.&lt;br /&gt;
* /usr/local/lib: where compiled libraries will reside.&lt;br /&gt;
* /usr/local/bin: where some executables will reside.&lt;br /&gt;
&lt;br /&gt;
Then, you must configure FreeTDS to point to your MSSQL DB server. To do so, edit (or create) the /usr/local/etc/freetds.conf file and put in there exclusively these lines:&lt;br /&gt;
&lt;br /&gt;
  [global]&lt;br /&gt;
      host = xxx.xxx.xxx.xxx (ip of the MSSQL server)&lt;br /&gt;
      port = 1433&lt;br /&gt;
      client charset = UTF-8&lt;br /&gt;
      tds version = 7.0 (or 8.0 if using FreeTDS 0.82 or later)&lt;br /&gt;
      text size = 20971520&lt;br /&gt;
&lt;br /&gt;
At this point, and previously to build the &#039;&#039;&#039;mssql extension alternative&#039;&#039;&#039;, you can test conectivity with your MSSQL DB using the &amp;quot;/usr/local/bin/tsql&amp;quot; executable. Just do this:&lt;br /&gt;
&lt;br /&gt;
  tsql -S serverhost -U dbowner -P dbpassword&lt;br /&gt;
&lt;br /&gt;
If everything is ok, you&#039;ll get this output:&lt;br /&gt;
&lt;br /&gt;
  locale is &amp;quot;es_ES.UTF-8&amp;quot;&lt;br /&gt;
  locale charset is &amp;quot;UTF-8&amp;quot;&lt;br /&gt;
  1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
just type, for example:&lt;br /&gt;
&lt;br /&gt;
  sp_help sysobjects&lt;br /&gt;
&lt;br /&gt;
and you might get some output from DB. Finally type:&lt;br /&gt;
&lt;br /&gt;
  exit&lt;br /&gt;
&lt;br /&gt;
and you&#039;ll be out from the &amp;quot;tsql&amp;quot; command line interpreter.&lt;br /&gt;
&lt;br /&gt;
Now that you&#039;ve successfully built, configured and tested FreeTDS it is time to create the &#039;&#039;&#039;mssql extension alternative&#039;&#039;&#039; that will provide us with the capacity of handling MSSQL DBs from within Moodle. To do so, you&#039;ll need configure your PHP server adding this new option to the usual ones:&lt;br /&gt;
&lt;br /&gt;
  --with-mssql=/usr/local/ &lt;br /&gt;
&lt;br /&gt;
then, after the standard &amp;quot;make and make install&amp;quot; steps, your PHP server will be built with MSSQL support provided by FreeTDS.&lt;br /&gt;
&lt;br /&gt;
Finally, configure your Moodle config.php with this DB related info and continue with a normal Moodle install:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$CFG-&amp;gt;dbtype    = &#039;mssql_n&#039;;         // Required&lt;br /&gt;
$CFG-&amp;gt;dbhost    = &#039;xxx.xxx.xxx.xxx&#039;; // IP of the MSSQL server (also proper hostname is allowed)&lt;br /&gt;
$CFG-&amp;gt;dbname    = &#039;moodle&#039;;          // or whatever you called the database you created&lt;br /&gt;
$CFG-&amp;gt;dbuser    = &#039;yourusername&#039;;    // I usually use the &#039;sa&#039; account (dbowner perms are enough)&lt;br /&gt;
$CFG-&amp;gt;dbpass    = &#039;yourpassword&#039;;&lt;br /&gt;
$CFG-&amp;gt;dbpersist =  false;&lt;br /&gt;
$CFG-&amp;gt;prefix    = &#039;mdl_&#039;;            //Prefix, you can change it, but NEVER leave it blank.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using FreeTDS on Windows ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p class=&amp;quot;note&amp;quot;&amp;gt;&#039;&#039;&#039;Important Note 1:&#039;&#039;&#039; Due to some previous bugs it&#039;s highly recommendable to use PHP &amp;gt;= 5.2.6 and FreeTDS 0.82 + post-release patches ([http://tracker.moodle.org/browse/MDL-14725 more info]).&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your web server is on Windows, use &#039;&#039;&#039;php_dblib.dll&#039;&#039;&#039;. Despite the name, it&#039;s FreeTDS compiled for Windows. &lt;br /&gt;
&lt;br /&gt;
Originally we were using the DLLs available at [http://kromann.info/article.php?Id=11062598797760000 Frank Kromann&#039;s site], but they are outdated (using old versions of FreeTDS) and that has caused [http://tracker.moodle.org/browse/MDL-14725 some problems] in the past.&lt;br /&gt;
&lt;br /&gt;
So, right now, the recommended way to use FreeTDS under Windows is to use PHP 5.2.x following the following instructions:&lt;br /&gt;
&lt;br /&gt;
1. Download the appropriate copy of php_dblib.dll from the list below, and save it into your /PHP/ext directory.&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; align=&amp;quot;center&amp;quot; cellpadding=&amp;quot;5&amp;quot; style=&amp;quot;text-align: center;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! PHP version !! [http://www.iis-aid.com/articles/my_word/difference_between_php_thread_safe_and_non_thread_safe_binaries Thread Safe]  !! FreeTDS version !! Download URL&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; | PHP 5.2.x || Yes || 0.82 + 20090302 patches || [http://download.moodle.org/download.php/dblib/php52/DBLIB_TS.zip Download!]&lt;br /&gt;
|-&lt;br /&gt;
| No || 0.82 + 20090302 patches || [http://download.moodle.org/download.php/dblib/php52/DBLIB_NOTS.zip Download!]&lt;br /&gt;
|-&lt;br /&gt;
| PHP 5.3.x || Both included || 0.82 + 20090904 patches || [http://tracker.moodle.org/secure/attachment/18402/php_dlib_ts-and-nts_php-5.3.x%29.zip Download!]&lt;br /&gt;
|-&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; |  Thanks to [http://remote-learner.net/ Remote-Learner]] (Moodle [http://moodle.com/partners/ Partner]) and specially to Bryan Williams, donating one Visual C++ 6.0 Pro license to Moodle. Thanks to Trevor Johnson and his builds of the dblib extensions. Thanks to Daniele, Doug, Luis, Sean and many others by their collaboration in MDL-14725. Thanks to Frediano Ziglio and James K. Lowden from [http://freetds.org freetds.org] by their support. Thanks!&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
(alternatively here you can find some [[Development:Compiling FreeTDS under Windows|instructions to build those freetds extensions under win32]] yourself)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2. FreeTDS requires the .NET Framework v1.1 to be installed.  You can [http://www.microsoft.com/downloads/details.aspx?FamilyID=262d25e3-f589-4842-8157-034d1e7cf3a3&amp;amp;DisplayLang=en download it from the Microsoft website] along with its [http://www.microsoft.com/downloads/details.aspx?FamilyID=a8f5654f-088e-40b2-bbdb-a83353618b38&amp;amp;DisplayLang=en service pack].  Alternatively, if you do not wish to install this framework, you can [http://kromann.info/ms-libs/msvcr71.dll download the required DLL] from Frank&#039;s site, and save it into your /PHP root directory.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3. Edit your /PHP/php.ini file and add this line:&lt;br /&gt;
&lt;br /&gt;
  extension=php_dblib.dll &lt;br /&gt;
&lt;br /&gt;
Make sure that any lines referring to the php_mssql.dll extension are DISABLED (commented out).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
4. Create a file called C:\freetds.conf with:&lt;br /&gt;
&lt;br /&gt;
  [global]&lt;br /&gt;
      host = xxx.xxx.xxx.xxx (ip of the MSSQL server)&lt;br /&gt;
      port = 1433&lt;br /&gt;
      client charset = UTF-8&lt;br /&gt;
      tds version = 8.0&lt;br /&gt;
      text size = 20971520&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
5. Your Moodle &#039;&#039;&#039;config.php&#039;&#039;&#039; should include lines like these:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$CFG-&amp;gt;dbtype    = &#039;mssql_n&#039;;        // Required&lt;br /&gt;
$CFG-&amp;gt;dbhost    = &#039;localhost&#039;;      // assuming MS SQL is on the same server, otherwise use an IP&lt;br /&gt;
$CFG-&amp;gt;dbname    = &#039;moodle&#039;;         // or whatever you called the database you created&lt;br /&gt;
$CFG-&amp;gt;dbuser    = &#039;yourusername&#039;;   // I usually use the &#039;sa&#039; account (dbowner perms are enough)&lt;br /&gt;
$CFG-&amp;gt;dbpass    = &#039;yourpassword&#039;;&lt;br /&gt;
$CFG-&amp;gt;dbpersist =  false;&lt;br /&gt;
$CFG-&amp;gt;prefix    = &#039;mdl_&#039;;            //Prefix, you can change it, but NEVER leave it blank.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t have a config.php file yet, it can be generated as normal from the Moodle installer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Restart or start your web server.  If Moodle still cannot communicate with the database server, please turn display_startup_errors to &amp;quot;On&amp;quot; in your /PHP/php.ini file, then restart the web server and check for any errors that may indicate incorrect DLL versions or missing dependencies.  These error reports, turned off by default in PHP, can be vital in locating a problem with new extension installations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
7. Database conection test, try this PHP script, just put in a text file called test.php change (&#039;localhost&#039;, &#039;db_user&#039;, &#039;db_password&#039;) to suite your setup, and load from local host (http://localhost/test.php)...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
	$link = mssql_connect(&#039;localhost&#039;, &#039;db_user&#039;, &#039;db_password&#039;);&lt;br /&gt;
	if(!$link) {&lt;br /&gt;
		echo&#039;Could not connect&#039;;&lt;br /&gt;
		die(&#039;Could not connect: &#039; . mssql_error());&lt;br /&gt;
	}&lt;br /&gt;
	echo&#039;Successful connection&#039;;&lt;br /&gt;
	mssql_close($link);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Install Moodle as usual.  Good luck!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Troubleshooting ===&lt;br /&gt;
If you encounter some problems you can try:&lt;br /&gt;
*check that you have DotNet framework 1.1 installed (later version are installed on Vista, but you could need this specific one)&amp;lt;br /&amp;gt;&lt;br /&gt;
*enable TCP/IP for MSSQL: SQL Server 2005 Network Configuration -&amp;gt; Protocols for MSSQLSERVER -&amp;gt; TCP/IP (Enable) -&amp;gt; Properties -&amp;gt; Ip Addresses -&amp;gt; 127.0.0.1 (Active+Enable)&amp;lt;br /&amp;gt;&lt;br /&gt;
*if you are using SQL Server 2005 and you have the error &#039;&#039;4004: Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier&#039;&#039;, try the ODBTP method (next chapter). The SQL Server complaining that it doesn&#039;t support pure Unicode via TDS or older versions of ODBC. Microsoft has deprecated DB-Library a long ago, in favor of ODBC, OLE DB, or SQL Native Client. Many new features of SQL 2005 aren&#039;t accessible via DB-Library so if you need them, you could have to switch away from tools based on TDS and DB-Library :(&lt;br /&gt;
&lt;br /&gt;
== Using ODBTP on Unix or Windows ==&lt;br /&gt;
&lt;br /&gt;
You can download ODBTP from http://odbtp.sourceforge.net/. Also you will access to the documentation from the same page.&lt;br /&gt;
&lt;br /&gt;
The downloaded package includes both the source code and some binaries to be installed in the server and some ready-to-use &#039;&#039;&#039;mssql extension alternatives&#039;&#039;&#039; for some platforms/PHP versions (so you won&#039;t need to compile it if your PHP server/version binary package is present).&lt;br /&gt;
&lt;br /&gt;
First of all, we have to install the Win32 service that comes with the package. Let&#039;s assume that it&#039;s going to run in the same Win32 machine where your MSSQL server is running (although it can run in any other Win32 server in your network).&lt;br /&gt;
&lt;br /&gt;
To do do, following the instructions present in http://odbtp.sourceforge.net/install.html, you must:&lt;br /&gt;
&lt;br /&gt;
Do the following on the MSSQL server:&lt;br /&gt;
# Create a directory on the Windows host where the service program files will reside, i.e., md odbtp.&lt;br /&gt;
# Copy the files odbtpctl.exe, odbtpsrv.exe and odbtpsrv.ini files from the winservice directory into the directory created in step 1.&lt;br /&gt;
# Edit the file odbtpsrv.ini of the previous step and this line: &amp;lt;pre&amp;gt;MaxRequestSize=20971520&amp;lt;/pre&amp;gt;&lt;br /&gt;
# Open a command prompt (cmd) window on the Windows host.&lt;br /&gt;
# Change to the directory to which the service program files were copied, i.e., cd odbtp.&lt;br /&gt;
# Run the following commands to install and start the service:&lt;br /&gt;
#*   odbtpctl install&lt;br /&gt;
#*   odbtpctl start&lt;br /&gt;
# With these steps you should have one new service running in your host called &amp;quot;odbtp&amp;quot;. Verify it&#039;s present and running in the &amp;quot;Services&amp;quot; control panel.&lt;br /&gt;
# Don&#039;t forget to enable TCP/IP incoming connections to port 2799 in the host you have installed the service!&lt;br /&gt;
&lt;br /&gt;
Now it&#039;s time to build the &#039;&#039;&#039;mssql extension alternative&#039;&#039;&#039;. First of all, verify if, in the downloaded package, under the &amp;quot;php&amp;quot; dir, there is one extension suitable for your PHP server/version. If it&#039;s present, you can simply copy it to the php/extensions dir in your PHP server and skip next points about compiling it from source. It&#039;s important to point that, inside each directory, you&#039;ll find &#039;&#039;&#039;two different&#039;&#039;&#039; libraries/dll files. The one that must be copied to the extensions dir is the one called &#039;&#039;&#039;&amp;quot;php_odbtp_mssql.xxx&amp;quot;&#039;&#039;&#039;!&lt;br /&gt;
&lt;br /&gt;
If in the downloaded package isn&#039;t present the extension matching your PHP platform/version, you should build if from source files. To do that, just &#039;&#039;&#039;&amp;quot;configure, make, make install&amp;quot;&#039;&#039;&#039;. That will create some stuff under &amp;quot;/usr/local&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
Now that you&#039;ve successfully built ODBTP is time to create the &#039;&#039;&#039;mssql extension alternative&#039;&#039;&#039; that will provide us with the capacity of handling MSSQL DBs from within Moodle. To do so, just configure your PHP server adding this new option to the usual ones:&lt;br /&gt;
&lt;br /&gt;
  --with-odbtp-mssql&lt;br /&gt;
&lt;br /&gt;
then, after the standard &amp;quot;make and make install&amp;quot; steps, your PHP server will be built with MSSQL support provided by ODBTP.&lt;br /&gt;
&lt;br /&gt;
Do the following on the moodle webserver:&lt;br /&gt;
Finally, independently if we are using the binary extension provided in the download or if you have built it from source files, it&#039;s time to configure the extension. &lt;br /&gt;
1. To do so, add this lines, if no present, to your php.ini file:&lt;br /&gt;
&lt;br /&gt;
  extension=php_odbtp_mssql.dll&lt;br /&gt;
&lt;br /&gt;
(only for Win32 PHP servers!)&lt;br /&gt;
&lt;br /&gt;
2. And, for all the server platforms: &lt;br /&gt;
&lt;br /&gt;
  [odbtp]&lt;br /&gt;
  odbtp.interface_file = &amp;quot;/path/to/your/odbtp.conf&amp;quot;&lt;br /&gt;
  odbtp.datetime_format = mdyhmsf&lt;br /&gt;
  odbtp.detach_default_queries = yes&lt;br /&gt;
&lt;br /&gt;
(where &#039;&#039;&amp;quot;/path/to/your/odbtp.conf&amp;quot;&#039;&#039; is usually &#039;&#039;&#039;&amp;quot;/usr/local/etc/odbtp.conf&amp;quot;&#039;&#039;&#039; for Unix systems and &#039;&#039;&#039;&amp;quot;C:\odbtp\odbtp.conf&amp;quot;&#039;&#039;&#039; for Windows systems)&lt;br /&gt;
&lt;br /&gt;
Then, edit such &amp;quot;odbtp.conf&amp;quot; file and put there these contents:&lt;br /&gt;
&lt;br /&gt;
  [global]&lt;br /&gt;
  odbtp host = xxx.xxx.xxx (ip or hostname of the Win32 box running the ODBTP service i.e MSSQL server)&lt;br /&gt;
  type = mssql&lt;br /&gt;
  unicode sql = yes&lt;br /&gt;
  use row cache = yes&lt;br /&gt;
  right trim text = yes&lt;br /&gt;
  var data size = 20971520&lt;br /&gt;
&lt;br /&gt;
With this, your PHP server will be able to connect with the MSSQL DB server using ODBTP. From here, just continue with the installation.&lt;br /&gt;
&lt;br /&gt;
Finally, if you find the ODBTP executables and &#039;&#039;&#039;mssql extension alternative&#039;&#039;&#039; in binary formats, it only will be necessary to install them in your server (binary packages...) without the need to recompile anything (just the php.ini and odbtp.conf edition steps above will be necessary). Of course, it will be really welcome to have all those binary alternatives documented here.&lt;br /&gt;
&lt;br /&gt;
Once ODBTP is working, Moodle config.php should include lines like these:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$CFG-&amp;gt;dbtype    = &#039;mssql_n&#039;;        // Required&lt;br /&gt;
$CFG-&amp;gt;dbhost    = &#039;localhost&#039;;      // assuming MS SQL is on the same server, otherwise use an IP&lt;br /&gt;
$CFG-&amp;gt;dbname    = &#039;moodle&#039;;         // or whatever you called the database you created&lt;br /&gt;
$CFG-&amp;gt;dbuser    = &#039;yourusername&#039;;   // I usually use the &#039;sa&#039; account (dbowner perms are enough)&lt;br /&gt;
$CFG-&amp;gt;dbpass    = &#039;yourpassword&#039;;&lt;br /&gt;
$CFG-&amp;gt;dbpersist =  false;&lt;br /&gt;
$CFG-&amp;gt;prefix    = &#039;mdl_&#039;;            //Prefix, you can change it, but NEVER leave it blank.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t have a config.php file yet, it can be generated as normal from the Moodle installer.&lt;br /&gt;
&lt;br /&gt;
== Using ODBC on Windows ==&lt;br /&gt;
[[ODBC]] allows communication with an SQL database.&lt;br /&gt;
{{Not for production sites}}&lt;br /&gt;
&lt;br /&gt;
1. Go to the &#039;&#039;&#039;Administrative Tools&#039;&#039;&#039;  control panel, then the &#039;&#039;&#039;Data Sources (ODBC)&#039;&#039;&#039; panel.&lt;br /&gt;
&lt;br /&gt;
2. Configure one new System/User DSN (call it, for example &amp;quot;moodle&amp;quot;). Dont forget to enable these options if the driver asks for them:&lt;br /&gt;
&lt;br /&gt;
:* ANSI NULLS Enabled = true&lt;br /&gt;
:* Quoted Identifiers Enabled = true&lt;br /&gt;
&lt;br /&gt;
3. Your Moodle config.php should include lines like these:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$CFG-&amp;gt;dbtype    = &#039;odbc_mssql&#039;;     // Note this is different to all the other configs on this page!&lt;br /&gt;
$CFG-&amp;gt;dbhost    = &#039;moodle&#039;;         // Where this matches the Data source name you chose above&lt;br /&gt;
$CFG-&amp;gt;dbname    = &#039;&#039;;               // Keep it blank!!&lt;br /&gt;
$CFG-&amp;gt;dbuser    = &#039;yourusername&#039;;   // I usually use the &#039;sa&#039; account (dbowner perms are enough)&lt;br /&gt;
$CFG-&amp;gt;dbpass    = &#039;yourpassword&#039;;&lt;br /&gt;
$CFG-&amp;gt;dbpersist =  false;&lt;br /&gt;
$CFG-&amp;gt;prefix    = &#039;mdl_&#039;;            //Prefix, you can change it, but NEVER leave it blank.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4. Install Moodle as usual.  Good luck!&lt;br /&gt;
&lt;br /&gt;
== Using the SQL Server 2005 Driver for PHP from Microsoft ==&lt;br /&gt;
In July 2008 Microsoft [http://social.msdn.microsoft.com/forums/en-US/sqldriverforphp/thread/a10e5202-9e41-4ff8-a33e-fbcc7b951be2/ released] their new SQL Server 2005 Driver for PHP. It is a PHP extension that allows for the reading and writing of SQL Server data from within PHP scripts. However there are some limitations with this driver that make it incompatible with Moodle, e.g.:&lt;br /&gt;
&lt;br /&gt;
* limitations with how it handles UTF-8 strings and &lt;br /&gt;
* it does not support the legacy mssql php driver function names&lt;br /&gt;
&lt;br /&gt;
For more info see MDL-16497 and MDL-15093.&lt;br /&gt;
&lt;br /&gt;
So, for now, you should not use this driver with Moodle 1.9.&lt;br /&gt;
&lt;br /&gt;
== Related links ==&lt;br /&gt;
&lt;br /&gt;
[[Installing Oracle for PHP]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Installation]]&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:XMLDB]]&lt;br /&gt;
[[Category:DB]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Compiling_FreeTDS_under_Windows&amp;diff=63151</id>
		<title>Compiling FreeTDS under Windows</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Compiling_FreeTDS_under_Windows&amp;diff=63151"/>
		<updated>2009-09-17T13:40:43Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Requirements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Requirements==&lt;br /&gt;
&lt;br /&gt;
* MSVC 6.0 (Microsoft Visual C++ 6.0) with Service Packs installed.&lt;br /&gt;
* FreeTDS (tested with [http://www.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-0.82.tar.gz version 0.82] + [http://freetds.sourceforge.net/post82.diff.gz post 0.82 patches] - updated 2009-03-02).&lt;br /&gt;
* PHP source files (tested with [http://www.php.net/get/php-5.2.6.tar.gz/from/a/mirror version 5.2.6])&lt;br /&gt;
* These packages (non-debug):&lt;br /&gt;
** From http://pecl2.php.net/downloads/php-windows-builds/php-libs/ :&lt;br /&gt;
*** binary-tools.zip&lt;br /&gt;
** From http://pecl2.php.net/downloads/php-windows-builds/php-libs/VC6/x86/ :&lt;br /&gt;
*** bindlib&lt;br /&gt;
*** libiconv&lt;br /&gt;
*** libxml&lt;br /&gt;
*** libxslt&lt;br /&gt;
*** zlib&lt;br /&gt;
&lt;br /&gt;
==Build Steps==&lt;br /&gt;
&lt;br /&gt;
* Create c:\dev&lt;br /&gt;
* Create c:\dev\php-build&lt;br /&gt;
* Uncompress all the packages listed in requirements and PHP into c:\dev\php-build (replacing all when uncompressing).&lt;br /&gt;
* Copy uncompressed [http://www.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-0.82.tar.gz freetds] folder to C:\dev\php-build (rename it to, simply, &amp;quot;freetds&amp;quot;).&lt;br /&gt;
* Apply [http://freetds.sourceforge.net/post82.diff.gz post 0.82 patches] in the &amp;quot;freetds&amp;quot; dir.&lt;br /&gt;
* Open the C:\dev\php-build\freetds\win32\msvc6\FreeTDS.dsw Project Workspace (it&#039;s really important to get this Workspace and not any of the individual projects!).&lt;br /&gt;
* In the &amp;quot;Build&amp;quot; menu, set the &amp;quot;Active Configuration&amp;quot; to  &amp;quot;dblib - Win32 Release&amp;quot; and then, in the same menu, &amp;quot;Rebuild All&amp;quot;. This should end with one dblib.lib library into C:\dev\php-build\freetd\win32\msvc6\db_Release&lt;br /&gt;
* Copy dblib.lib to C:\dev\php-build\lib&lt;br /&gt;
* Start CMD&lt;br /&gt;
* Create one C:\dev\prepare4php.bat file with contents below and execute it:&lt;br /&gt;
::@set PATH=C:\dev\php-build\bin;%PATH%&lt;br /&gt;
::@set INCLUDE=C:\dev\php-build\include;%INCLUDE%&lt;br /&gt;
::@set LIB=C:\dev\php-build\lib;%LIB%&lt;br /&gt;
::@set BISON_SIMPLE=C:\dev\php-build\bin\bison.simple&lt;br /&gt;
* Continue in CMD and change dir to C:\dev\php-build\php-x-x-x&lt;br /&gt;
* Execute this:&lt;br /&gt;
** buildconf&lt;br /&gt;
** cscript /nologo configure.js --disable-all --disable-ipv6  --enable-zts (--disable-zts)  --with-dblib=shared --enable-object-out-dir=c:\dev --with-extra-includes=c:\dev\php-build\freetds\include;c:\dev\php-build\freetds\win32&lt;br /&gt;
** nmake&lt;br /&gt;
* You should end with one C:\dev\Release_TS for the --enable-zts (or C:\dev\Release for the --disable-zts alternative)  dir, with your compiled FreeTDS PHP module ready at the root level of that dir.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
* By using --enable-zts or --disable-zts you&#039;ll end with different thread safe/no safe versions of the extension. Use them depending of your environment thread safety.&lt;br /&gt;
* If you one use PHP 5.2 version to build the lib, the extensions generated are expected to work against any PHP 5.2.x version (but not against other releases of PHP, like 5.1 or 5.3).&lt;br /&gt;
* MSVC 6.0 is required because it&#039;s the official tool used to build PHP binary distributions. It seems that, with PHP 5.3 they will start using MSVC 9 or so... corresponding extensions should use the same.&lt;br /&gt;
* Feel free to fix and improve this document. TIA! &lt;br /&gt;
* For any comment related to this, please use MDL-14725 in the Moodle Tracker.&lt;br /&gt;
* And MDL-11810 has a related discussion.&lt;br /&gt;
&lt;br /&gt;
[[Category:Installation]]&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:XMLDB]]&lt;br /&gt;
[[Category:DB]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55118</id>
		<title>Broken/Blocks</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55118"/>
		<updated>2009-04-30T10:38:33Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Lists and Icons */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; A Step-by-step Guide To Creating Blocks &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Original Author: Jon Papaioannou (pj@moodle.org)&lt;br /&gt;
&lt;br /&gt;
The present document serves as a guide to developers who want to create their own blocks for use in Moodle. It applies to the 1.5 development version of Moodle (and any newer) &#039;&#039;&#039;only&#039;&#039;&#039;, as the blocks subsystem was rewritten and expanded for the 1.5 release. However, you can also find it useful if you want to modify blocks written for Moodle 1.3 and 1.4 to work with the latest versions (look at [[Development:Blocks/Appendix_B| Appendix B]]).&lt;br /&gt;
&lt;br /&gt;
The guide is written as an interactive course which aims to develop a configurable, multi-purpose block that displays arbitrary HTML. It&#039;s targeted mainly at people with little experience with Moodle or programming in general and aims to show how easy it is to create new blocks for Moodle. A certain small amount of PHP programming knowledge is still required, though. &lt;br /&gt;
&lt;br /&gt;
Experienced developers and those who just want a reference text should refer to [[Development:Blocks/Appendix_A| Appendix A]] because the main guide has a rather low concentration of pure information in the text.&lt;br /&gt;
&lt;br /&gt;
== Basic Concepts ==&lt;br /&gt;
&lt;br /&gt;
Through this guide, we will be following the creation of an &amp;quot;HTML&amp;quot; block from scratch in order to demonstrate most of the block features at our disposal. Our block will be named &amp;quot;SimpleHTML&amp;quot;. This does not constrain us regarding the name of the actual directory on the server where the files for our block will be stored, but for consistency we will follow the practice of using the lowercased form &amp;quot;simplehtml&amp;quot; in any case where such a name is required. &lt;br /&gt;
&lt;br /&gt;
Whenever we refer to a file or directory name which contains &amp;quot;simplehtml&amp;quot;, it&#039;s important to remember that &#039;&#039;only&#039;&#039; the &amp;quot;simplehtml&amp;quot; part is up to us to change; the rest is standardized and essential for Moodle to work correctly.&lt;br /&gt;
&lt;br /&gt;
Whenever a file&#039;s path is mentioned in this guide, it will always start with a slash. This refers to the Moodle home directory; all files and directories will be referred to with respect to that directory.&lt;br /&gt;
&lt;br /&gt;
== Ready, Set, Go! ==&lt;br /&gt;
&lt;br /&gt;
To define a &amp;quot;block&amp;quot; in Moodle, in the most basic case we need to provide just one source code file. We start by creating the directory &#039;&#039;/blocks/simplehtml/&#039;&#039; and creating a file named &#039;&#039;/blocks/simplehtml/&#039;&#039;&#039;&#039;&#039;block_simplehtml.php&#039;&#039;&#039; which will hold our code. We then begin coding the block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class block_simplehtml extends block_base {&lt;br /&gt;
  function init() {&lt;br /&gt;
    $this-&amp;gt;title   = get_string(&#039;simplehtml&#039;, &#039;block_simplehtml&#039;);&lt;br /&gt;
    $this-&amp;gt;version = 2004111200;&lt;br /&gt;
  }&lt;br /&gt;
  // The PHP tag and the curly bracket for the class definition &lt;br /&gt;
  // will only be closed after there is another function added in the next section.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first line is our block class definition; it must be named exactly in the manner shown. Again, only the &amp;quot;simplehtml&amp;quot; part can (and indeed must) change; everything else is standardized.&lt;br /&gt;
&lt;br /&gt;
Our class is then given a small method: [[Development:Blocks/Appendix_A#init.28.29| init()]]. This is essential for all blocks, and its purpose is to set the two class member variables listed inside it. But what do these values actually mean? Here&#039;s a more detailed description.&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Etitle| $this-&amp;gt;title]] is the title displayed in the header of our block. We can set it to whatever we like; in this case it&#039;s set to read the actual title from a language file we are presumably distributing together with the block. I &#039;ll skip ahead a bit here and say that if you want your block to display &#039;&#039;&#039;no&#039;&#039;&#039; title at all, then you should set this to any descriptive value you want (but &#039;&#039;&#039;not&#039;&#039;&#039; make it an empty string). We will later see [[Development:Blocks#Eye_Candy| how to disable the title&#039;s display]].&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] is the version of our block. This actually would only make a difference if your block wanted to keep its own data in special tables in the database (i.e. for very complex blocks). In that case the version number is used exactly as it&#039;s used in activities; an upgrade script uses it to incrementally upgrade an &amp;quot;old&amp;quot; version of the block&#039;s data to the latest. We will outline this process further ahead, since blocks tend to be relatively simple and not hold their own private data. &lt;br /&gt;
&lt;br /&gt;
In our example, this is certainly the case so we just set [[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] to &#039;&#039;&#039;YYYYMMDD00&#039;&#039;&#039; and forget about it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039;&amp;lt;br /&amp;gt; &lt;br /&gt;
Prior to version 1.5, the basic structure of each block class was slightly different. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== I Just Hear Static ==&lt;br /&gt;
In order to get our block to actually display something on screen, we need to add one more method to our class (before the final closing brace in our file). The new code is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
  function get_content() {&lt;br /&gt;
    if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
      return $this-&amp;gt;content;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;content         =  new stdClass;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
}   // Here&#039;s the closing curly bracket for the class definition&lt;br /&gt;
    // and here&#039;s the closing PHP tag from the section above.&lt;br /&gt;
?&amp;gt;  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can&#039;t get any simpler than that, can it? Let&#039;s dissect this method to see what&#039;s going on...&lt;br /&gt;
&lt;br /&gt;
First of all, there is a check that returns the current value of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] if it&#039;s not NULL; otherwise we proceed with &amp;quot;computing&amp;quot; it. Since the computation is potentially a time-consuming operation and it &#039;&#039;&#039;will&#039;&#039;&#039; be called several times for each block (Moodle works that way internally), we take a precaution and include this time-saver.&lt;br /&gt;
Supposing the content had not been computed before (it was NULL), we then define it from scratch. The code speaks for itself there, so there isn&#039;t much to say. Just keep in mind that we can use HTML both in the text &#039;&#039;&#039;and&#039;&#039;&#039; in the footer, if we want to.&lt;br /&gt;
&lt;br /&gt;
At this point our block should be capable of being automatically installed in Moodle and added to courses; visit your administration page to install it (Click &amp;quot;Notifications&amp;quot; under the Site Administration Block) and after seeing it in action come back to continue our tutorial.&lt;br /&gt;
&lt;br /&gt;
== Configure That Out ==&lt;br /&gt;
&lt;br /&gt;
The current version of our block doesn&#039;t really do much; it just displays a fixed message, which is not very useful. What we &#039;d really like to do is allow the teachers to customize what goes into the block. This, in block-speak, is called &amp;quot;instance configuration&amp;quot;. So let&#039;s give our block some instance configuration...&lt;br /&gt;
First of all, we need to tell Moodle that we want it to provide instance-specific configuration amenities to our block. That&#039;s as simple as adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This small change is enough to make Moodle display an &amp;quot;Edit...&amp;quot; icon in our block&#039;s header when we turn editing mode on in any course. However, if you try to click on that icon you will be presented with a notice that complains about the block&#039;s configuration not being implemented correctly. Try it, it&#039;s harmless.&lt;br /&gt;
Moodle&#039;s complaints do make sense. We told it that we want to have configuration, but we didn&#039;t say &#039;&#039;what&#039;&#039; kind of configuration we want, or how it should be displayed. To do that, we need to create one more file: &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/&#039;&#039;&#039;config_instance.html&#039;&#039;&#039;&amp;lt;/span&amp;gt; (which has to be named exactly like that). For the moment, copy paste the following into it and save:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;table cellpadding=&amp;quot;9&amp;quot; cellspacing=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_string(&#039;configcontent&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_textarea(true, 10, 50, 0, 0, &#039;text&#039;, $this-&amp;gt;config-&amp;gt;text); ?&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td colspan=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;) ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php use_html_editor(); ?&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It isn&#039;t difficult to see that the above code just provides us with a wysiwyg-editor-enabled textarea to write our block&#039;s desired content in and a submit button to save. But... what&#039;s $this-&amp;gt;config-&amp;gt;text? Well...&lt;br /&gt;
Moodle goes a long way to make things easier for block developers. Did you notice that the textarea is actually named &amp;quot;text&amp;quot;? When the submit button is pressed, Moodle saves each and every field it can find in our &#039;&#039;&#039;config_instance.html&#039;&#039;&#039; file as instance configuration data. &lt;br /&gt;
&lt;br /&gt;
We can then access that data as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;&#039;&#039;variablename&#039;&#039;&#039;&#039;&#039;, where &#039;&#039;variablename&#039;&#039; is the actual name we used for our field; in this case, &amp;quot;text&amp;quot;. So in essence, the above form just pre-populates the textarea with the current content of the block (as indeed it should) and then allows us to change it.&lt;br /&gt;
&lt;br /&gt;
You also might be surprised by the presence of a submit button and the absence of any &amp;lt;form&amp;gt; element at the same time. But the truth is, we don&#039;t need to worry about that at all; Moodle goes a really long way to make things easier for developers! We just print the configuration options we want, in any format we want; include a submit button, and Moodle will handle all the rest itself. The instance configuration variables are automatically at our disposal to access from any of the class methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29| init()]].&lt;br /&gt;
&lt;br /&gt;
In the event where the default behavior is not satisfactory, we can still override it. However, this requires advanced modifications to our block class and will not be covered here; refer to [[Development:Blocks/Appendix_A| Appendix A]] for more details.&lt;br /&gt;
Having now the ability to refer to this instance configuration data through [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]], the final twist is to tell our block to actually &#039;&#039;display&#039;&#039; what is saved in its configuration data. To do that, find this snippet in &#039;&#039;/blocks/simplehtml/block_simplehtml.php&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and change it to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Oh, and since the footer isn&#039;t really exciting at this point, we remove it from our block because it doesn&#039;t contribute anything. We could just as easily have decided to make the footer configurable in the above way, too. So for our latest code, the snippet becomes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After this discussion, our block is ready for prime time! Indeed, if you now visit any course with a SimpleHTML block, you will see that modifying its contents is now a snap.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Specialists ==&lt;br /&gt;
&lt;br /&gt;
Implementing instance configuration for the block&#039;s contents was good enough to whet our appetite, but who wants to stop there? Why not customize the block&#039;s title, too?&lt;br /&gt;
&lt;br /&gt;
Why not, indeed. Well, our first attempt to achieve this is natural enough: let&#039;s add another field to &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_instance.html&amp;lt;/span&amp;gt;. Here goes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&amp;lt;p&amp;gt;&lt;br /&gt;
    &amp;lt;?php print_string(&#039;configtitle&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;title&amp;quot; size=&amp;quot;30&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;config-&amp;gt;title; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We save the edited file, go to a course, edit the title of the block and... nothing happens! The instance configuration is saved correctly, all right (editing it once more proves that) but it&#039;s not being displayed. All we get is just the simple &amp;quot;SimpleHTML&amp;quot; title.&lt;br /&gt;
&lt;br /&gt;
That&#039;s not too weird, if we think back a bit. Do you remember that [[Development:Blocks/Appendix_A#init.28.29|init()]] method, where we set [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]]? We didn&#039;t actually change its value from then, and [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] is definitely not the same as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;title&#039;&#039;&#039; (to Moodle, at least). What we need is a way to update [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] with the value in the instance configuration. But as we said a bit earlier, we can use [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]] in all methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29|init()]]! So what can we do?&lt;br /&gt;
&lt;br /&gt;
Let&#039;s pull out another ace from our sleeve, and add this small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function specialization() {&lt;br /&gt;
  if(!empty($this-&amp;gt;config-&amp;gt;title)){&lt;br /&gt;
    $this-&amp;gt;title = $this-&amp;gt;config-&amp;gt;title;&lt;br /&gt;
  }else{&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;title = &#039;Some title ...&#039;;&lt;br /&gt;
  }&lt;br /&gt;
  if(empty($this-&amp;gt;config-&amp;gt;text)){&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;text = &#039;Some text ...&#039;;&lt;br /&gt;
  }    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Aha, here&#039;s what we wanted to do all along! But what&#039;s going on with the [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method?&lt;br /&gt;
&lt;br /&gt;
This &amp;quot;magic&amp;quot; method has actually a very nice property: it&#039;s &#039;&#039;guaranteed&#039;&#039; to be automatically called by Moodle as soon as our instance configuration is loaded and available (that is, immediately after [[Development:Blocks/Appendix_A#init.28.29|init()]] is called). That means before the block&#039;s content is computed for the first time, and indeed before &#039;&#039;anything&#039;&#039; else is done with the block. Thus, providing a [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method is the natural choice for any configuration data that needs to be acted upon &amp;quot;as soon as possible&amp;quot;, as in this case.&lt;br /&gt;
&lt;br /&gt;
== Now You See Me, Now You Don&#039;t ==&lt;br /&gt;
&lt;br /&gt;
Now would be a good time to mention another nifty technique that can be used in blocks, and which comes in handy quite often. Specifically, it may be the case that our block will have something interesting to display some of the time; but in some other cases, it won&#039;t have anything useful to say. (An example here would be the &amp;quot;Recent Activity&amp;quot; block, in the case where no recent activity in fact exists. &lt;br /&gt;
&lt;br /&gt;
However in that case the block chooses to explicitly inform you of the lack of said activity, which is arguably useful). It would be nice, then, to be able to have our block &amp;quot;disappear&amp;quot; if it&#039;s not needed to display it.&lt;br /&gt;
&lt;br /&gt;
This is indeed possible, and the way to do it is to make sure that after the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called, the block is completely void of content. Specifically, &amp;quot;void of content&amp;quot; means that both $this-&amp;gt;content-&amp;gt;text and $this-&amp;gt;content-&amp;gt;footer are each equal to the empty string (&amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt;). Moodle performs this check by calling the block&#039;s [[Development:Blocks/Appendix_A#is_empty.28.29| is_empty()]] method, and if the block is indeed empty then it is not displayed at all.&lt;br /&gt;
&lt;br /&gt;
Note that the exact value of the block&#039;s title and the presence or absence of a [[Development:Blocks/Appendix_A#hide_header.28.29| hide_header()]] method do &#039;&#039;not&#039;&#039; affect this behavior. A block is considered empty if it has no content, irrespective of anything else.&lt;br /&gt;
&lt;br /&gt;
== We Are Legion ==&lt;br /&gt;
&lt;br /&gt;
Right now our block is fully configurable, both in title and content. It&#039;s so versatile, in fact, that we could make pretty much anything out of it. It would be really nice to be able to add multiple blocks of this type to a single course. And, as you might have guessed, doing that is as simple as adding another small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_multiple() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Moodle that it should allow any number of instances of the SimpleHTML block in any course. After saving the changes to our file, Moodle immediately allows us to add multiple copies of the block without further ado!&lt;br /&gt;
&lt;br /&gt;
There are a couple more of interesting points to note here. First of all, even if a block itself allows multiple instances in the same page, the administrator still has the option of disallowing such behavior. This setting can be set separately for each block from the Administration / Configuration / Blocks page.&lt;br /&gt;
&lt;br /&gt;
And finally, a nice detail is that as soon as we defined an [[Development:Blocks/Appendix_A#instance_allow_multiple.28.29| instance_allow_multiple()]] method, the method [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] that was already defined became obsolete. &lt;br /&gt;
&lt;br /&gt;
Moodle assumes that if a block allows multiple instances of itself, those instances will want to be configured (what is the point of same multiple instances in the same page if they are identical?) and thus automatically provides an &amp;quot;Edit&amp;quot; icon. So, we can also remove the whole [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] method now without harm. We had only needed it when multiple instances of the block were not allowed.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Effects of Globalization ==&lt;br /&gt;
&lt;br /&gt;
Configuring each block instance with its own personal data is cool enough, but sometimes administrators need some way to &amp;quot;touch&amp;quot; all instances of a specific block at the same time. In the case of our SimpleHTML block, a few settings that would make sense to apply to all instances aren&#039;t that hard to come up with. &lt;br /&gt;
&lt;br /&gt;
For example, we might want to limit the contents of each block to only so many characters, or we might have a setting that filters HTML out of the block&#039;s contents, only allowing pure text in. Granted, such a feature wouldn&#039;t win us any awards for naming our block &amp;quot;SimpleHTML&amp;quot; but some tormented administrator somewhere might actually find it useful.&lt;br /&gt;
&lt;br /&gt;
This kind of configuration is called &amp;quot;global configuration&amp;quot; and applies only to a specific block type (all instances of that block type are affected, however). Implementing such configuration for our block is quite similar to implementing the instance configuration. We will now see how to implement the second example, having a setting that only allows text and not HTML in the block&#039;s contents.&lt;br /&gt;
First of all, we need to tell Moodle that we want our block to provide global configuration by, what a surprise, adding a small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function has_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we need to create a HTML file that actually prints out the configuration screen. In our case, we &#039;ll just print out a checkbox saying &amp;quot;Do not allow HTML in the content&amp;quot; and a &amp;quot;submit&amp;quot; button. Let&#039;s create the file &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_global.html&amp;lt;/span&amp;gt; which again must be named just so, and copy paste the following into it:&lt;br /&gt;
&lt;br /&gt;
[[Development_talk:Blocks|TODO: New settings.php method]] &lt;br /&gt;
: Just to note that general documentation about admin settings is at [[Development:Admin_settings#Individual_settings]]. In the absence of documentation, you can look at blocks/course_list, blocks/online_users and blocks/rss_client. They all use a settings.php file.--[[User:Tim Hunt|Tim Hunt]] 19:38, 28 January 2009 (CST)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;1&amp;quot;&lt;br /&gt;
   &amp;lt;?php if(!empty($CFG-&amp;gt;block_simplehtml_strict)) &lt;br /&gt;
             echo &#039;checked=&amp;quot;checked&amp;quot;&#039;; ?&amp;gt; /&amp;gt;&lt;br /&gt;
   &amp;lt;?php print_string(&#039;donotallowhtml&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;&lt;br /&gt;
 &amp;lt;p&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;); ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
True to our block&#039;s name, this looks simple enough. What it does is that it displays a checkbox named &amp;quot;block_simplehtml_strict&amp;quot; and if the Moodle configuration variable with the same name (i.e., $CFG-&amp;gt;block_simplehtml_strict) is set and not empty (that means it&#039;s not equal to an empty string, to zero, or to boolean FALSE) it displays the box as pre-checked (reflecting the current status). &lt;br /&gt;
&lt;br /&gt;
Why does it check the configuration setting with the same name? Because the default implementation of the global configuration saving code takes all the variables we have in our form and saves them as Moodle configuration options with the same name. Thus, it&#039;s good practice to use a descriptive name and also one that won&#039;t possibly conflict with the name of another setting. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;block_simplehtml_strict&amp;quot; clearly satisfies both requirements.&lt;br /&gt;
&lt;br /&gt;
The astute reader may have noticed that we actually have &#039;&#039;two&#039;&#039; input fields named &amp;quot;block_simplehtml_strict&amp;quot; in our configuration file. One is hidden and its value is always 0; the other is the checkbox and its value is 1. What gives? Why have them both there?&lt;br /&gt;
&lt;br /&gt;
Actually, this is a small trick we use to make our job as simple as possible. HTML forms work this way: if a checkbox in a form is not checked, its name does not appear at all in the variables passed to PHP when the form is submitted. That effectively means that, when we uncheck the box and click submit, the variable is not passed to PHP at all. Thus, PHP does not know to update its value to &amp;quot;0&amp;quot;, and our &amp;quot;strict&amp;quot; setting cannot be turned off at all once we turn it on for the first time. Not the behavior we want, surely.&lt;br /&gt;
&lt;br /&gt;
However, when PHP handles received variables from a form, the variables are processed in the order in which they appear in the form. If a variable comes up having the same name with an already-processed variable, the new value overwrites the old one. Taking advantage of this, our logic runs as follows: the variable &amp;quot;block_simplehtml_strict&amp;quot; is first unconditionally set to &amp;quot;0&amp;quot;. Then, &#039;&#039;if&#039;&#039; the box is checked, it is set to &amp;quot;1&amp;quot;, overwriting the previous value as discussed. The net result is that our configuration setting behaves as it should.&lt;br /&gt;
&lt;br /&gt;
To round our bag of tricks up, notice that the use of &#039;&#039;if(!empty($CFG-&amp;gt;block_simplehtml_strict))&#039;&#039; in the test for &amp;quot;should the box be checked by default?&amp;quot; is quite deliberate. The first time this script runs, the variable &#039;&#039;&#039;$CFG-&amp;gt;block_simplehtml_strict&#039;&#039;&#039; will not exist at all. After it&#039;s set for the first time, its value can be either &amp;quot;0&amp;quot; or &amp;quot;1&amp;quot;. Given that both &amp;quot;not set&amp;quot; and the string &amp;quot;0&amp;quot; evaluate as empty while the sting &amp;quot;1&amp;quot; does not, we manage to avoid any warnings from PHP regarding the variable not being set at all, &#039;&#039;and&#039;&#039; have a nice human-readable representation for its two possible values (&amp;quot;0&amp;quot; and &amp;quot;1&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
=== config_save() ===&lt;br /&gt;
&lt;br /&gt;
Now that we have managed to cram a respectable amount of tricks into a few lines of HTML, we might as well discuss the alternative in case that tricks are not enough for a specific configuration setup we have in mind. Saving the data is done in the method [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], the default implementation of which is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  // Default behavior: save all variables as $CFG properties&lt;br /&gt;
  foreach ($data as $name =&amp;gt; $value) {&lt;br /&gt;
    set_config($name, $value);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As can be clearly seen, Moodle passes this method an associative array $data which contains all the variables coming in from our configuration screen. If we wanted to do the job without the &amp;quot;hidden variable with the same name&amp;quot; trick we used above, one way to do it would be by overriding this method with the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  if(isset($data[&#039;block_simplehtml_strict&#039;])) {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;1&#039;);&lt;br /&gt;
  }else {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;0&#039;);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quite straightfoward: if the variable &amp;quot;block_simplehtml_strict&amp;quot; is passed to us, then it can only mean that the user has checked it, so set the configuration variable with the same name to &amp;quot;1&amp;quot;. Otherwise, set it to &amp;quot;0&amp;quot;. Of course, this version would need to be updated if we add more configuration options because it doesn&#039;t respond to them as the default implementation does. Still, it&#039;s useful to know how we can override the default implementation if it does not fit our needs (for example, we might not want to save the variable as part of the Moodle configuration but do something else with it).&lt;br /&gt;
&lt;br /&gt;
So, we are now at the point where we know if the block should allow HTML tags in its content or not. How do we get the block to actually respect that setting?&lt;br /&gt;
&lt;br /&gt;
We could decide to do one of two things: either have the block &amp;quot;clean&amp;quot; HTML out from the input before saving it in the instance configuration and then display it as-is (the &amp;quot;eager&amp;quot; approach); or have it save the data &amp;quot;as is&amp;quot; and then clean it up each time just before displaying it (the &amp;quot;lazy&amp;quot; approach). The eager approach involves doing work once when saving the configuration; the lazy approach means doing work each time the block is displayed and thus it promises to be worse performance-wise. We shall hence go with the eager approach.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
=== instance_config_save() ===&lt;br /&gt;
&lt;br /&gt;
Much as we did just before with overriding [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], what is needed here is overriding the method [[Development:Blocks/Appendix_A#instance_config_save.28.29| instance_config_save()]] which handles the instance configuration. The default implementation is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  $data = stripslashes_recursive($data);&lt;br /&gt;
  $this-&amp;gt;config = $data;&lt;br /&gt;
  return set_field(&#039;block_instance&#039;, &lt;br /&gt;
                   &#039;configdata&#039;,&lt;br /&gt;
                    base64_encode(serialize($data)),&lt;br /&gt;
                   &#039;id&#039;, &lt;br /&gt;
                   $this-&amp;gt;instance-&amp;gt;id);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This may look intimidating at first (what&#039;s all this stripslashes_recursive() and base64_encode() and serialize() stuff?) but do not despair; we won&#039;t have to touch any of it. We will only add some extra validation code in the beginning and then instruct Moodle to additionally call this default implementation to do the actual storing of the data. Specifically, we will add a method to our class which goes like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  // Clean the data if we have to&lt;br /&gt;
  global $CFG;&lt;br /&gt;
  if(!empty($CFG-&amp;gt;block_simplehtml_strict)) {&lt;br /&gt;
    $data-&amp;gt;text = strip_tags($data-&amp;gt;text);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // And now forward to the default implementation defined in the parent class&lt;br /&gt;
  return parent::instance_config_save($data);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At last! Now the administrator has absolute power of life and death over what type of content is allowed in our &amp;quot;SimpleHTML&amp;quot; block! Absolute? Well... not exactly. In fact, if we think about it for a while, it will become apparent that if at some point in time HTML is allowed and some blocks have saved their content with HTML included, and afterwards the administrator changes the setting to &amp;quot;off&amp;quot;, this will only prevent subsequent content changes from including HTML. Blocks which already had HTML in their content would continue to display it!&lt;br /&gt;
&lt;br /&gt;
Following that train of thought, the next stop is realizing that we wouldn&#039;t have this problem if we had chosen the lazy approach a while back, because in that case we would &amp;quot;sanitize&amp;quot; each block&#039;s content just before it was displayed. &lt;br /&gt;
&lt;br /&gt;
The only thing we can do with the eager approach is strip all the tags from the content of all SimpleHTML instances as soon as the admin setting is changed to &amp;quot;HTML off&amp;quot;; but even then, turning the setting back to &amp;quot;HTML on&amp;quot; won&#039;t bring back the tags we stripped away. On the other hand, the lazy approach might be slower, but it&#039;s more versatile; we can choose whether to strip or keep the HTML before displaying the content, and we won&#039;t lose it at all if the admin toggles the setting off and on again. Isn&#039;t the life of a developer simple and wonderful?&lt;br /&gt;
&lt;br /&gt;
=== Exercise === &lt;br /&gt;
We will let this part of the tutorial come to a close with the obligatory exercise for the reader: &lt;br /&gt;
In order to have the SimpleHTML block work &amp;quot;correctly&amp;quot;, find out how to strengthen the eager approach to strip out all tags from the existing configuration of all instances of our block, &#039;&#039;&#039;or&#039;&#039;&#039; go back and implement the lazy approach instead. &lt;br /&gt;
(Hint: Do that in the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method.)&lt;br /&gt;
&lt;br /&gt;
=== UPDATING: === &lt;br /&gt;
Prior to version 1.5, the file &#039;&#039;config_global.html&#039;&#039; was named simply &#039;&#039;config.html&#039;&#039;. Also, the methods [[Blocks_Howto#method_config_save| config_save]] and [[Blocks_Howto#method_config_print| config_print]] were named &#039;&#039;&#039;handle_config&#039;&#039;&#039; and &#039;&#039;&#039;print_config&#039;&#039;&#039; respectively. Upgrading a block to work with Moodle 1.5 involves updating these aspects; refer to [[Blocks_Howto#appendix_b| Appendix B]] for more information.&lt;br /&gt;
&lt;br /&gt;
== Eye Candy ==&lt;br /&gt;
&lt;br /&gt;
Our block is just about complete functionally, so now let&#039;s take a look at some of the tricks we can use to make its behavior customized in a few more useful ways.&lt;br /&gt;
&lt;br /&gt;
First of all, there are a couple of ways we can adjust the visual aspects of our block. For starters, it might be useful to create a block that doesn&#039;t display a header (title) at all. You can see this effect in action in the Course Description block that comes with Moodle. This behavior is achieved by, you guessed it, adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function hide_header() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One more note here: we cannot just set an empty title inside the block&#039;s [[Development:Blocks/Appendix_A#init.28.29| init()]] method; it&#039;s necessary for each block to have a unique, non-empty title after [[Development:Blocks/Appendix_A#init.28.29| init()]] is called so that Moodle can use those titles to differentiate between all of the installed blocks.&lt;br /&gt;
&lt;br /&gt;
Another adjustment we might want to do is instruct our block to take up a certain amount of width on screen. Moodle handles this as a two-part process: first, it queries each block about its preferred width and takes the maximum number as the desired value. Then, the page that&#039;s being displayed can choose to use this value or, more probably, bring it within some specific range of values if it isn&#039;t already. That means that the width setting is a best-effort settlement; your block can &#039;&#039;request&#039;&#039; a certain width and Moodle will &#039;&#039;try&#039;&#039; to provide it, but there&#039;s no guarantee whatsoever about the end result. As a concrete example, all standard Moodle course formats will deliver any requested width between 180 and 210 pixels, inclusive.&lt;br /&gt;
&lt;br /&gt;
To instruct Moodle about our block&#039;s preferred width, we add one more method to the block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function preferred_width() {&lt;br /&gt;
  // The preferred value is in pixels&lt;br /&gt;
  return 200;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This will make our block (and all the other blocks displayed at the same side of the page) a bit wider than standard.&lt;br /&gt;
&lt;br /&gt;
Finally, we can also affect some properties of the actual HTML that will be used to print our block. Each block is fully contained within a &amp;amp;lt;table&amp;amp;gt; element, inside which all the HTML for that block is printed. We can instruct Moodle to add HTML attributes with specific values to that container. This would be done to either a) directly affect the end result (if we say, assign bgcolor=&amp;quot;black&amp;quot;), or b) give us freedom to customize the end result using CSS (this is in fact done by default as we &#039;ll see below).&lt;br /&gt;
&lt;br /&gt;
The default behavior of this feature in our case will assign to our block&#039;s container the class HTML attribute with the value &amp;quot;sideblock block_simplehtml&amp;quot; (the prefix &amp;quot;block_&amp;quot; followed by the name of our block, lowercased). We can then use that class to make CSS selectors in our theme to alter this block&#039;s visual style (for example, &amp;quot;.sideblock.block_simplehtml { border: 1px black solid}&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To change the default behavior, we will need to define a method which returns an associative array of attribute names and values. For example, the version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
  return array(&lt;br /&gt;
    &#039;class&#039;       =&amp;gt; &#039;sideblock block_&#039;. $this-&amp;gt;name(),&lt;br /&gt;
    &#039;onmouseover&#039; =&amp;gt; &amp;quot;alert(&#039;Mouseover on our block!&#039;);&amp;quot;&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will result in a mouseover event being added to our block using JavaScript, just as if we had written the onmouseover=&amp;quot;alert(...)&amp;quot; part ourselves in HTML. Note that we actually duplicate the part which sets the class attribute (we want to keep that, and since we override the default behavior it&#039;s our responsibility to emulate it if required). &lt;br /&gt;
&lt;br /&gt;
And the final elegant touch is that we don&#039;t set the class to the hard-coded value &amp;quot;block_simplehtml&amp;quot; but instead use the [[Development:Blocks/Appendix_A#name.28.29| name()]] method to make it dynamically match our block&#039;s name.&lt;br /&gt;
&lt;br /&gt;
== Authorized Personnel Only ==&lt;br /&gt;
&lt;br /&gt;
It&#039;s not difficult to imagine a block which is very useful in some circumstances but it simply cannot be made meaningful in others. An example of this would be the &amp;quot;Social Activities&amp;quot; block which is indeed useful in a course with the social format, but doesn&#039;t do anything useful in a course with the weeks format. There should be some way of allowing the use of such blocks only where they are indeed meaningful, and not letting them confuse users if they are not.&lt;br /&gt;
&lt;br /&gt;
Moodle allows us to declare which course formats each block is allowed to be displayed in, and enforces these restrictions as set by the block developers at all times. The information is given to Moodle as a standard associative array, with each key corresponding to a page format and defining a boolean value (true/false) that declares whether the block should be allowed to appear in that page format.&lt;br /&gt;
&lt;br /&gt;
Notice the deliberate use of the term &#039;&#039;page&#039;&#039; instead of &#039;&#039;course&#039;&#039; in the above paragraph. This is because in Moodle 1.5 and onwards, blocks can be displayed in any page that supports them. The best example of such pages are the course pages, but we are not restricted to them. For instance, the quiz view page (the first one we see when we click on the name of the quiz) also supports blocks.&lt;br /&gt;
&lt;br /&gt;
The format names we can use for the pages derive from the name of the script which is actually used to display that page. For example, when we are looking at a course, the script is &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/course/view.php&amp;lt;/span&amp;gt; (this is evident from the browser&#039;s address line). Thus, the format name of that page is &#039;&#039;&#039;course-view&#039;&#039;&#039;. It follows easily that the format name for a quiz view page is &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039;. This rule of thumb does have a few exceptions, however:&lt;br /&gt;
&lt;br /&gt;
# The format name for the front page of Moodle is &#039;&#039;&#039;site-index&#039;&#039;&#039;.&lt;br /&gt;
# The format name for courses is actually not just &#039;&#039;&#039;course-view&#039;&#039;&#039;&amp;lt;nowiki&amp;gt;; it is &amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;course-view-weeks&#039;&#039;&#039;, &#039;&#039;&#039;course-view-topics&#039;&#039;&#039;, etc.&lt;br /&gt;
# Even though there is no such page, the format name &#039;&#039;&#039;all&#039;&#039;&#039; can be used as a catch-all option.&lt;br /&gt;
&lt;br /&gt;
We can include as many format names as we want in our definition of the applicable formats. Each format can be allowed or disallowed, and there are also three more rules that help resolve the question &amp;quot;is this block allowed into this page or not?&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
# Prefixes of a format name will match that format name; for example, &#039;&#039;&#039;mod&#039;&#039;&#039; will match all the activity modules. &#039;&#039;&#039;course-view&#039;&#039;&#039; will match any course, regardless of the course format. And finally, &#039;&#039;&#039;site&#039;&#039;&#039; will also match the front page (remember that its full format name is &#039;&#039;&#039;site-index&#039;&#039;&#039;).&lt;br /&gt;
# The more specialized a format name that matches our page is, the higher precedence it has when deciding if the block will be allowed. For example, &#039;&#039;&#039;mod&#039;&#039;&#039;, &#039;&#039;&#039;mod-quiz&#039;&#039;&#039; and &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; all match the quiz view page. But if all three are present, &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; will take precedence over the other two because it is a better match.&lt;br /&gt;
# The character &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; can be used in place of any word. For example, &#039;&#039;&#039;mod&#039;&#039;&#039; and &#039;&#039;&#039;mod-*&#039;&#039;&#039; are equivalent. At the time of this document&#039;s writing, there is no actual reason to utilize this &amp;quot;wildcard matching&amp;quot; feature, but it exists for future usage.&lt;br /&gt;
# The order that the format names appear does not make any difference.&lt;br /&gt;
All of the above are enough to make the situation sound complex, so let&#039;s look at some specific examples. First of all, to have our block appear &#039;&#039;&#039;only&#039;&#039;&#039; in the site front page, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&#039;site&#039; =&amp;gt; true);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Since &#039;&#039;&#039;all&#039;&#039;&#039; is missing, the block is disallowed from appearing in &#039;&#039;any&#039;&#039; course format; but then &#039;&#039;&#039;site&#039;&#039;&#039; is set to TRUE, so it&#039;s explicitly allowed to appear in the site front page (remember that &#039;&#039;&#039;site&#039;&#039;&#039; matches &#039;&#039;&#039;site-index&#039;&#039;&#039; because it&#039;s a prefix).&lt;br /&gt;
&lt;br /&gt;
For another example, if we wanted to allow the block to appear in all course formats &#039;&#039;except&#039;&#039; social, and also to &#039;&#039;not&#039;&#039; be allowed anywhere but in courses, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;course-view&#039; =&amp;gt; true, &lt;br /&gt;
    &#039;course-view-social&#039; =&amp;gt; false);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This time, we first allow the block to appear in all courses and then we explicitly disallow the social format.&lt;br /&gt;
For our final, most complicated example, suppose that a block can be displayed in the site front page, in courses (but not social courses) and also when we are viewing any activity module, &#039;&#039;except&#039;&#039; quiz. This would be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;site-index&#039; =&amp;gt; true,&lt;br /&gt;
          &#039;course-view&#039; =&amp;gt; true, &lt;br /&gt;
   &#039;course-view-social&#039; =&amp;gt; false,&lt;br /&gt;
                  &#039;mod&#039; =&amp;gt; true, &lt;br /&gt;
             &#039;mod-quiz&#039; =&amp;gt; false&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is not difficult to realize that the above accomplishes the objective if we remember that there is a &amp;quot;best match&amp;quot; policy to determine the end result.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039; &amp;lt;br /&amp;gt;&lt;br /&gt;
Prior to version 1.5, blocks were only allowed in courses (and in Moodle 1.4, in the site front page). Also, the keywords used to describe the valid course formats at the time were slightly different and had to be changed in order to allow for a more open architecture. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== Lists and Icons ==&lt;br /&gt;
&lt;br /&gt;
In this final part of the guide we will briefly discuss an additional capability of Moodle&#039;s block system, namely the ability to very easily create blocks that display a list of choices to the user. This list is displayed with one item per line, and an optional image (icon) next to the item. An example of such a &#039;&#039;list block&#039;&#039; is the standard Moodle &amp;quot;admin&amp;quot; block, which illustrates all the points discussed in this section.&lt;br /&gt;
&lt;br /&gt;
As we have seen so far, blocks use two properties of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]]: &amp;quot;text&amp;quot; and &amp;quot;footer&amp;quot;. The text is displayed as-is as the block content, and the footer is displayed below the content in a smaller font size. List blocks use $this-&amp;gt;content-&amp;gt;footer in the exact same way, but they ignore $this-&amp;gt;content-&amp;gt;text.&lt;br /&gt;
&lt;br /&gt;
Instead, Moodle expects such blocks to set two other properties when the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called: $this-&amp;gt;content-&amp;gt;items and $this-&amp;gt;content-&amp;gt;icons. $this-&amp;gt;content-&amp;gt;items should be a numerically indexed array containing elements that represent the HTML for each item in the list that is going to be displayed. Usually these items will be HTML anchor tags which provide links to some page. $this-&amp;gt;content-&amp;gt;icons should also be a numerically indexed array, with exactly as many items as $this-&amp;gt;content-&amp;gt;items has. Each of these items should be a fully qualified HTML &amp;lt;img&amp;gt; tag, with &amp;quot;src&amp;quot;, &amp;quot;height&amp;quot;, &amp;quot;width&amp;quot; and &amp;quot;alt&amp;quot; attributes. Obviously, it makes sense to keep the images small and of a uniform size.&lt;br /&gt;
&lt;br /&gt;
In order to tell Moodle that we want to have a list block instead of the standard text block, we need to make a small change to our block class declaration. Instead of extending class &#039;&#039;&#039;block_base&#039;&#039;&#039;, our block will extend class &#039;&#039;&#039;block_list&#039;&#039;&#039;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
 class block_my_menu extends block_list {&lt;br /&gt;
     // The init() method does not need to change at all&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to making this change, we must of course also modify the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method to construct the [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] variable as discussed above:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function get_content() {&lt;br /&gt;
  if ($this-&amp;gt;content !== null) {&lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content         = new stdClass;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items[] = &#039;&amp;lt;a href=&amp;quot;some_file.php&amp;quot;&amp;gt;Menu Option 1&amp;lt;/a&amp;gt;&#039;;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons[] = &#039;&amp;lt;img src=&amp;quot;images/icons/1.gif&amp;quot; class=&amp;quot;icon&amp;quot; alt=&amp;quot;&amp;quot; /&amp;gt;&#039;;&lt;br /&gt;
 &lt;br /&gt;
  // Add more list items here&lt;br /&gt;
 &lt;br /&gt;
  return $this-&amp;gt;content;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To summarize, if we want to create a list block instead of a text block, we just need to change the block class declaration and the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method. Adding the mandatory [[Development:Blocks/Appendix_A#init.28.29| init()]] method as discussed earlier will then give us our first list block in no time!&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== Appendices ==&lt;br /&gt;
&lt;br /&gt;
The appendices have been moved to separate pages:&lt;br /&gt;
&lt;br /&gt;
* Appendix A: [[Development:Blocks/Appendix A|&#039;&#039;block_base&#039;&#039; Reference]] &lt;br /&gt;
* Appendix B: [[Development:Blocks/Appendix B|Differences in the Blocks API for Moodle Versions prior to 1.5]]&lt;br /&gt;
* Appendix C: [[Development:Blocks/Appendix C|Creating Database Tables for Blocks (prior to 1.7)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Blocks]]&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
&lt;br /&gt;
[[es:Desarrollo de bloques]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55117</id>
		<title>Broken/Blocks</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55117"/>
		<updated>2009-04-30T10:38:22Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Authorized Personnel Only */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; A Step-by-step Guide To Creating Blocks &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Original Author: Jon Papaioannou (pj@moodle.org)&lt;br /&gt;
&lt;br /&gt;
The present document serves as a guide to developers who want to create their own blocks for use in Moodle. It applies to the 1.5 development version of Moodle (and any newer) &#039;&#039;&#039;only&#039;&#039;&#039;, as the blocks subsystem was rewritten and expanded for the 1.5 release. However, you can also find it useful if you want to modify blocks written for Moodle 1.3 and 1.4 to work with the latest versions (look at [[Development:Blocks/Appendix_B| Appendix B]]).&lt;br /&gt;
&lt;br /&gt;
The guide is written as an interactive course which aims to develop a configurable, multi-purpose block that displays arbitrary HTML. It&#039;s targeted mainly at people with little experience with Moodle or programming in general and aims to show how easy it is to create new blocks for Moodle. A certain small amount of PHP programming knowledge is still required, though. &lt;br /&gt;
&lt;br /&gt;
Experienced developers and those who just want a reference text should refer to [[Development:Blocks/Appendix_A| Appendix A]] because the main guide has a rather low concentration of pure information in the text.&lt;br /&gt;
&lt;br /&gt;
== Basic Concepts ==&lt;br /&gt;
&lt;br /&gt;
Through this guide, we will be following the creation of an &amp;quot;HTML&amp;quot; block from scratch in order to demonstrate most of the block features at our disposal. Our block will be named &amp;quot;SimpleHTML&amp;quot;. This does not constrain us regarding the name of the actual directory on the server where the files for our block will be stored, but for consistency we will follow the practice of using the lowercased form &amp;quot;simplehtml&amp;quot; in any case where such a name is required. &lt;br /&gt;
&lt;br /&gt;
Whenever we refer to a file or directory name which contains &amp;quot;simplehtml&amp;quot;, it&#039;s important to remember that &#039;&#039;only&#039;&#039; the &amp;quot;simplehtml&amp;quot; part is up to us to change; the rest is standardized and essential for Moodle to work correctly.&lt;br /&gt;
&lt;br /&gt;
Whenever a file&#039;s path is mentioned in this guide, it will always start with a slash. This refers to the Moodle home directory; all files and directories will be referred to with respect to that directory.&lt;br /&gt;
&lt;br /&gt;
== Ready, Set, Go! ==&lt;br /&gt;
&lt;br /&gt;
To define a &amp;quot;block&amp;quot; in Moodle, in the most basic case we need to provide just one source code file. We start by creating the directory &#039;&#039;/blocks/simplehtml/&#039;&#039; and creating a file named &#039;&#039;/blocks/simplehtml/&#039;&#039;&#039;&#039;&#039;block_simplehtml.php&#039;&#039;&#039; which will hold our code. We then begin coding the block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class block_simplehtml extends block_base {&lt;br /&gt;
  function init() {&lt;br /&gt;
    $this-&amp;gt;title   = get_string(&#039;simplehtml&#039;, &#039;block_simplehtml&#039;);&lt;br /&gt;
    $this-&amp;gt;version = 2004111200;&lt;br /&gt;
  }&lt;br /&gt;
  // The PHP tag and the curly bracket for the class definition &lt;br /&gt;
  // will only be closed after there is another function added in the next section.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first line is our block class definition; it must be named exactly in the manner shown. Again, only the &amp;quot;simplehtml&amp;quot; part can (and indeed must) change; everything else is standardized.&lt;br /&gt;
&lt;br /&gt;
Our class is then given a small method: [[Development:Blocks/Appendix_A#init.28.29| init()]]. This is essential for all blocks, and its purpose is to set the two class member variables listed inside it. But what do these values actually mean? Here&#039;s a more detailed description.&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Etitle| $this-&amp;gt;title]] is the title displayed in the header of our block. We can set it to whatever we like; in this case it&#039;s set to read the actual title from a language file we are presumably distributing together with the block. I &#039;ll skip ahead a bit here and say that if you want your block to display &#039;&#039;&#039;no&#039;&#039;&#039; title at all, then you should set this to any descriptive value you want (but &#039;&#039;&#039;not&#039;&#039;&#039; make it an empty string). We will later see [[Development:Blocks#Eye_Candy| how to disable the title&#039;s display]].&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] is the version of our block. This actually would only make a difference if your block wanted to keep its own data in special tables in the database (i.e. for very complex blocks). In that case the version number is used exactly as it&#039;s used in activities; an upgrade script uses it to incrementally upgrade an &amp;quot;old&amp;quot; version of the block&#039;s data to the latest. We will outline this process further ahead, since blocks tend to be relatively simple and not hold their own private data. &lt;br /&gt;
&lt;br /&gt;
In our example, this is certainly the case so we just set [[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] to &#039;&#039;&#039;YYYYMMDD00&#039;&#039;&#039; and forget about it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039;&amp;lt;br /&amp;gt; &lt;br /&gt;
Prior to version 1.5, the basic structure of each block class was slightly different. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== I Just Hear Static ==&lt;br /&gt;
In order to get our block to actually display something on screen, we need to add one more method to our class (before the final closing brace in our file). The new code is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
  function get_content() {&lt;br /&gt;
    if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
      return $this-&amp;gt;content;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;content         =  new stdClass;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
}   // Here&#039;s the closing curly bracket for the class definition&lt;br /&gt;
    // and here&#039;s the closing PHP tag from the section above.&lt;br /&gt;
?&amp;gt;  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can&#039;t get any simpler than that, can it? Let&#039;s dissect this method to see what&#039;s going on...&lt;br /&gt;
&lt;br /&gt;
First of all, there is a check that returns the current value of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] if it&#039;s not NULL; otherwise we proceed with &amp;quot;computing&amp;quot; it. Since the computation is potentially a time-consuming operation and it &#039;&#039;&#039;will&#039;&#039;&#039; be called several times for each block (Moodle works that way internally), we take a precaution and include this time-saver.&lt;br /&gt;
Supposing the content had not been computed before (it was NULL), we then define it from scratch. The code speaks for itself there, so there isn&#039;t much to say. Just keep in mind that we can use HTML both in the text &#039;&#039;&#039;and&#039;&#039;&#039; in the footer, if we want to.&lt;br /&gt;
&lt;br /&gt;
At this point our block should be capable of being automatically installed in Moodle and added to courses; visit your administration page to install it (Click &amp;quot;Notifications&amp;quot; under the Site Administration Block) and after seeing it in action come back to continue our tutorial.&lt;br /&gt;
&lt;br /&gt;
== Configure That Out ==&lt;br /&gt;
&lt;br /&gt;
The current version of our block doesn&#039;t really do much; it just displays a fixed message, which is not very useful. What we &#039;d really like to do is allow the teachers to customize what goes into the block. This, in block-speak, is called &amp;quot;instance configuration&amp;quot;. So let&#039;s give our block some instance configuration...&lt;br /&gt;
First of all, we need to tell Moodle that we want it to provide instance-specific configuration amenities to our block. That&#039;s as simple as adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This small change is enough to make Moodle display an &amp;quot;Edit...&amp;quot; icon in our block&#039;s header when we turn editing mode on in any course. However, if you try to click on that icon you will be presented with a notice that complains about the block&#039;s configuration not being implemented correctly. Try it, it&#039;s harmless.&lt;br /&gt;
Moodle&#039;s complaints do make sense. We told it that we want to have configuration, but we didn&#039;t say &#039;&#039;what&#039;&#039; kind of configuration we want, or how it should be displayed. To do that, we need to create one more file: &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/&#039;&#039;&#039;config_instance.html&#039;&#039;&#039;&amp;lt;/span&amp;gt; (which has to be named exactly like that). For the moment, copy paste the following into it and save:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;table cellpadding=&amp;quot;9&amp;quot; cellspacing=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_string(&#039;configcontent&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_textarea(true, 10, 50, 0, 0, &#039;text&#039;, $this-&amp;gt;config-&amp;gt;text); ?&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td colspan=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;) ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php use_html_editor(); ?&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It isn&#039;t difficult to see that the above code just provides us with a wysiwyg-editor-enabled textarea to write our block&#039;s desired content in and a submit button to save. But... what&#039;s $this-&amp;gt;config-&amp;gt;text? Well...&lt;br /&gt;
Moodle goes a long way to make things easier for block developers. Did you notice that the textarea is actually named &amp;quot;text&amp;quot;? When the submit button is pressed, Moodle saves each and every field it can find in our &#039;&#039;&#039;config_instance.html&#039;&#039;&#039; file as instance configuration data. &lt;br /&gt;
&lt;br /&gt;
We can then access that data as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;&#039;&#039;variablename&#039;&#039;&#039;&#039;&#039;, where &#039;&#039;variablename&#039;&#039; is the actual name we used for our field; in this case, &amp;quot;text&amp;quot;. So in essence, the above form just pre-populates the textarea with the current content of the block (as indeed it should) and then allows us to change it.&lt;br /&gt;
&lt;br /&gt;
You also might be surprised by the presence of a submit button and the absence of any &amp;lt;form&amp;gt; element at the same time. But the truth is, we don&#039;t need to worry about that at all; Moodle goes a really long way to make things easier for developers! We just print the configuration options we want, in any format we want; include a submit button, and Moodle will handle all the rest itself. The instance configuration variables are automatically at our disposal to access from any of the class methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29| init()]].&lt;br /&gt;
&lt;br /&gt;
In the event where the default behavior is not satisfactory, we can still override it. However, this requires advanced modifications to our block class and will not be covered here; refer to [[Development:Blocks/Appendix_A| Appendix A]] for more details.&lt;br /&gt;
Having now the ability to refer to this instance configuration data through [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]], the final twist is to tell our block to actually &#039;&#039;display&#039;&#039; what is saved in its configuration data. To do that, find this snippet in &#039;&#039;/blocks/simplehtml/block_simplehtml.php&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and change it to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Oh, and since the footer isn&#039;t really exciting at this point, we remove it from our block because it doesn&#039;t contribute anything. We could just as easily have decided to make the footer configurable in the above way, too. So for our latest code, the snippet becomes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After this discussion, our block is ready for prime time! Indeed, if you now visit any course with a SimpleHTML block, you will see that modifying its contents is now a snap.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Specialists ==&lt;br /&gt;
&lt;br /&gt;
Implementing instance configuration for the block&#039;s contents was good enough to whet our appetite, but who wants to stop there? Why not customize the block&#039;s title, too?&lt;br /&gt;
&lt;br /&gt;
Why not, indeed. Well, our first attempt to achieve this is natural enough: let&#039;s add another field to &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_instance.html&amp;lt;/span&amp;gt;. Here goes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&amp;lt;p&amp;gt;&lt;br /&gt;
    &amp;lt;?php print_string(&#039;configtitle&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;title&amp;quot; size=&amp;quot;30&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;config-&amp;gt;title; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We save the edited file, go to a course, edit the title of the block and... nothing happens! The instance configuration is saved correctly, all right (editing it once more proves that) but it&#039;s not being displayed. All we get is just the simple &amp;quot;SimpleHTML&amp;quot; title.&lt;br /&gt;
&lt;br /&gt;
That&#039;s not too weird, if we think back a bit. Do you remember that [[Development:Blocks/Appendix_A#init.28.29|init()]] method, where we set [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]]? We didn&#039;t actually change its value from then, and [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] is definitely not the same as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;title&#039;&#039;&#039; (to Moodle, at least). What we need is a way to update [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] with the value in the instance configuration. But as we said a bit earlier, we can use [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]] in all methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29|init()]]! So what can we do?&lt;br /&gt;
&lt;br /&gt;
Let&#039;s pull out another ace from our sleeve, and add this small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function specialization() {&lt;br /&gt;
  if(!empty($this-&amp;gt;config-&amp;gt;title)){&lt;br /&gt;
    $this-&amp;gt;title = $this-&amp;gt;config-&amp;gt;title;&lt;br /&gt;
  }else{&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;title = &#039;Some title ...&#039;;&lt;br /&gt;
  }&lt;br /&gt;
  if(empty($this-&amp;gt;config-&amp;gt;text)){&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;text = &#039;Some text ...&#039;;&lt;br /&gt;
  }    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Aha, here&#039;s what we wanted to do all along! But what&#039;s going on with the [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method?&lt;br /&gt;
&lt;br /&gt;
This &amp;quot;magic&amp;quot; method has actually a very nice property: it&#039;s &#039;&#039;guaranteed&#039;&#039; to be automatically called by Moodle as soon as our instance configuration is loaded and available (that is, immediately after [[Development:Blocks/Appendix_A#init.28.29|init()]] is called). That means before the block&#039;s content is computed for the first time, and indeed before &#039;&#039;anything&#039;&#039; else is done with the block. Thus, providing a [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method is the natural choice for any configuration data that needs to be acted upon &amp;quot;as soon as possible&amp;quot;, as in this case.&lt;br /&gt;
&lt;br /&gt;
== Now You See Me, Now You Don&#039;t ==&lt;br /&gt;
&lt;br /&gt;
Now would be a good time to mention another nifty technique that can be used in blocks, and which comes in handy quite often. Specifically, it may be the case that our block will have something interesting to display some of the time; but in some other cases, it won&#039;t have anything useful to say. (An example here would be the &amp;quot;Recent Activity&amp;quot; block, in the case where no recent activity in fact exists. &lt;br /&gt;
&lt;br /&gt;
However in that case the block chooses to explicitly inform you of the lack of said activity, which is arguably useful). It would be nice, then, to be able to have our block &amp;quot;disappear&amp;quot; if it&#039;s not needed to display it.&lt;br /&gt;
&lt;br /&gt;
This is indeed possible, and the way to do it is to make sure that after the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called, the block is completely void of content. Specifically, &amp;quot;void of content&amp;quot; means that both $this-&amp;gt;content-&amp;gt;text and $this-&amp;gt;content-&amp;gt;footer are each equal to the empty string (&amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt;). Moodle performs this check by calling the block&#039;s [[Development:Blocks/Appendix_A#is_empty.28.29| is_empty()]] method, and if the block is indeed empty then it is not displayed at all.&lt;br /&gt;
&lt;br /&gt;
Note that the exact value of the block&#039;s title and the presence or absence of a [[Development:Blocks/Appendix_A#hide_header.28.29| hide_header()]] method do &#039;&#039;not&#039;&#039; affect this behavior. A block is considered empty if it has no content, irrespective of anything else.&lt;br /&gt;
&lt;br /&gt;
== We Are Legion ==&lt;br /&gt;
&lt;br /&gt;
Right now our block is fully configurable, both in title and content. It&#039;s so versatile, in fact, that we could make pretty much anything out of it. It would be really nice to be able to add multiple blocks of this type to a single course. And, as you might have guessed, doing that is as simple as adding another small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_multiple() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Moodle that it should allow any number of instances of the SimpleHTML block in any course. After saving the changes to our file, Moodle immediately allows us to add multiple copies of the block without further ado!&lt;br /&gt;
&lt;br /&gt;
There are a couple more of interesting points to note here. First of all, even if a block itself allows multiple instances in the same page, the administrator still has the option of disallowing such behavior. This setting can be set separately for each block from the Administration / Configuration / Blocks page.&lt;br /&gt;
&lt;br /&gt;
And finally, a nice detail is that as soon as we defined an [[Development:Blocks/Appendix_A#instance_allow_multiple.28.29| instance_allow_multiple()]] method, the method [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] that was already defined became obsolete. &lt;br /&gt;
&lt;br /&gt;
Moodle assumes that if a block allows multiple instances of itself, those instances will want to be configured (what is the point of same multiple instances in the same page if they are identical?) and thus automatically provides an &amp;quot;Edit&amp;quot; icon. So, we can also remove the whole [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] method now without harm. We had only needed it when multiple instances of the block were not allowed.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Effects of Globalization ==&lt;br /&gt;
&lt;br /&gt;
Configuring each block instance with its own personal data is cool enough, but sometimes administrators need some way to &amp;quot;touch&amp;quot; all instances of a specific block at the same time. In the case of our SimpleHTML block, a few settings that would make sense to apply to all instances aren&#039;t that hard to come up with. &lt;br /&gt;
&lt;br /&gt;
For example, we might want to limit the contents of each block to only so many characters, or we might have a setting that filters HTML out of the block&#039;s contents, only allowing pure text in. Granted, such a feature wouldn&#039;t win us any awards for naming our block &amp;quot;SimpleHTML&amp;quot; but some tormented administrator somewhere might actually find it useful.&lt;br /&gt;
&lt;br /&gt;
This kind of configuration is called &amp;quot;global configuration&amp;quot; and applies only to a specific block type (all instances of that block type are affected, however). Implementing such configuration for our block is quite similar to implementing the instance configuration. We will now see how to implement the second example, having a setting that only allows text and not HTML in the block&#039;s contents.&lt;br /&gt;
First of all, we need to tell Moodle that we want our block to provide global configuration by, what a surprise, adding a small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function has_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we need to create a HTML file that actually prints out the configuration screen. In our case, we &#039;ll just print out a checkbox saying &amp;quot;Do not allow HTML in the content&amp;quot; and a &amp;quot;submit&amp;quot; button. Let&#039;s create the file &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_global.html&amp;lt;/span&amp;gt; which again must be named just so, and copy paste the following into it:&lt;br /&gt;
&lt;br /&gt;
[[Development_talk:Blocks|TODO: New settings.php method]] &lt;br /&gt;
: Just to note that general documentation about admin settings is at [[Development:Admin_settings#Individual_settings]]. In the absence of documentation, you can look at blocks/course_list, blocks/online_users and blocks/rss_client. They all use a settings.php file.--[[User:Tim Hunt|Tim Hunt]] 19:38, 28 January 2009 (CST)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;1&amp;quot;&lt;br /&gt;
   &amp;lt;?php if(!empty($CFG-&amp;gt;block_simplehtml_strict)) &lt;br /&gt;
             echo &#039;checked=&amp;quot;checked&amp;quot;&#039;; ?&amp;gt; /&amp;gt;&lt;br /&gt;
   &amp;lt;?php print_string(&#039;donotallowhtml&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;&lt;br /&gt;
 &amp;lt;p&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;); ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
True to our block&#039;s name, this looks simple enough. What it does is that it displays a checkbox named &amp;quot;block_simplehtml_strict&amp;quot; and if the Moodle configuration variable with the same name (i.e., $CFG-&amp;gt;block_simplehtml_strict) is set and not empty (that means it&#039;s not equal to an empty string, to zero, or to boolean FALSE) it displays the box as pre-checked (reflecting the current status). &lt;br /&gt;
&lt;br /&gt;
Why does it check the configuration setting with the same name? Because the default implementation of the global configuration saving code takes all the variables we have in our form and saves them as Moodle configuration options with the same name. Thus, it&#039;s good practice to use a descriptive name and also one that won&#039;t possibly conflict with the name of another setting. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;block_simplehtml_strict&amp;quot; clearly satisfies both requirements.&lt;br /&gt;
&lt;br /&gt;
The astute reader may have noticed that we actually have &#039;&#039;two&#039;&#039; input fields named &amp;quot;block_simplehtml_strict&amp;quot; in our configuration file. One is hidden and its value is always 0; the other is the checkbox and its value is 1. What gives? Why have them both there?&lt;br /&gt;
&lt;br /&gt;
Actually, this is a small trick we use to make our job as simple as possible. HTML forms work this way: if a checkbox in a form is not checked, its name does not appear at all in the variables passed to PHP when the form is submitted. That effectively means that, when we uncheck the box and click submit, the variable is not passed to PHP at all. Thus, PHP does not know to update its value to &amp;quot;0&amp;quot;, and our &amp;quot;strict&amp;quot; setting cannot be turned off at all once we turn it on for the first time. Not the behavior we want, surely.&lt;br /&gt;
&lt;br /&gt;
However, when PHP handles received variables from a form, the variables are processed in the order in which they appear in the form. If a variable comes up having the same name with an already-processed variable, the new value overwrites the old one. Taking advantage of this, our logic runs as follows: the variable &amp;quot;block_simplehtml_strict&amp;quot; is first unconditionally set to &amp;quot;0&amp;quot;. Then, &#039;&#039;if&#039;&#039; the box is checked, it is set to &amp;quot;1&amp;quot;, overwriting the previous value as discussed. The net result is that our configuration setting behaves as it should.&lt;br /&gt;
&lt;br /&gt;
To round our bag of tricks up, notice that the use of &#039;&#039;if(!empty($CFG-&amp;gt;block_simplehtml_strict))&#039;&#039; in the test for &amp;quot;should the box be checked by default?&amp;quot; is quite deliberate. The first time this script runs, the variable &#039;&#039;&#039;$CFG-&amp;gt;block_simplehtml_strict&#039;&#039;&#039; will not exist at all. After it&#039;s set for the first time, its value can be either &amp;quot;0&amp;quot; or &amp;quot;1&amp;quot;. Given that both &amp;quot;not set&amp;quot; and the string &amp;quot;0&amp;quot; evaluate as empty while the sting &amp;quot;1&amp;quot; does not, we manage to avoid any warnings from PHP regarding the variable not being set at all, &#039;&#039;and&#039;&#039; have a nice human-readable representation for its two possible values (&amp;quot;0&amp;quot; and &amp;quot;1&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
=== config_save() ===&lt;br /&gt;
&lt;br /&gt;
Now that we have managed to cram a respectable amount of tricks into a few lines of HTML, we might as well discuss the alternative in case that tricks are not enough for a specific configuration setup we have in mind. Saving the data is done in the method [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], the default implementation of which is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  // Default behavior: save all variables as $CFG properties&lt;br /&gt;
  foreach ($data as $name =&amp;gt; $value) {&lt;br /&gt;
    set_config($name, $value);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As can be clearly seen, Moodle passes this method an associative array $data which contains all the variables coming in from our configuration screen. If we wanted to do the job without the &amp;quot;hidden variable with the same name&amp;quot; trick we used above, one way to do it would be by overriding this method with the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  if(isset($data[&#039;block_simplehtml_strict&#039;])) {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;1&#039;);&lt;br /&gt;
  }else {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;0&#039;);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quite straightfoward: if the variable &amp;quot;block_simplehtml_strict&amp;quot; is passed to us, then it can only mean that the user has checked it, so set the configuration variable with the same name to &amp;quot;1&amp;quot;. Otherwise, set it to &amp;quot;0&amp;quot;. Of course, this version would need to be updated if we add more configuration options because it doesn&#039;t respond to them as the default implementation does. Still, it&#039;s useful to know how we can override the default implementation if it does not fit our needs (for example, we might not want to save the variable as part of the Moodle configuration but do something else with it).&lt;br /&gt;
&lt;br /&gt;
So, we are now at the point where we know if the block should allow HTML tags in its content or not. How do we get the block to actually respect that setting?&lt;br /&gt;
&lt;br /&gt;
We could decide to do one of two things: either have the block &amp;quot;clean&amp;quot; HTML out from the input before saving it in the instance configuration and then display it as-is (the &amp;quot;eager&amp;quot; approach); or have it save the data &amp;quot;as is&amp;quot; and then clean it up each time just before displaying it (the &amp;quot;lazy&amp;quot; approach). The eager approach involves doing work once when saving the configuration; the lazy approach means doing work each time the block is displayed and thus it promises to be worse performance-wise. We shall hence go with the eager approach.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
=== instance_config_save() ===&lt;br /&gt;
&lt;br /&gt;
Much as we did just before with overriding [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], what is needed here is overriding the method [[Development:Blocks/Appendix_A#instance_config_save.28.29| instance_config_save()]] which handles the instance configuration. The default implementation is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  $data = stripslashes_recursive($data);&lt;br /&gt;
  $this-&amp;gt;config = $data;&lt;br /&gt;
  return set_field(&#039;block_instance&#039;, &lt;br /&gt;
                   &#039;configdata&#039;,&lt;br /&gt;
                    base64_encode(serialize($data)),&lt;br /&gt;
                   &#039;id&#039;, &lt;br /&gt;
                   $this-&amp;gt;instance-&amp;gt;id);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This may look intimidating at first (what&#039;s all this stripslashes_recursive() and base64_encode() and serialize() stuff?) but do not despair; we won&#039;t have to touch any of it. We will only add some extra validation code in the beginning and then instruct Moodle to additionally call this default implementation to do the actual storing of the data. Specifically, we will add a method to our class which goes like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  // Clean the data if we have to&lt;br /&gt;
  global $CFG;&lt;br /&gt;
  if(!empty($CFG-&amp;gt;block_simplehtml_strict)) {&lt;br /&gt;
    $data-&amp;gt;text = strip_tags($data-&amp;gt;text);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // And now forward to the default implementation defined in the parent class&lt;br /&gt;
  return parent::instance_config_save($data);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At last! Now the administrator has absolute power of life and death over what type of content is allowed in our &amp;quot;SimpleHTML&amp;quot; block! Absolute? Well... not exactly. In fact, if we think about it for a while, it will become apparent that if at some point in time HTML is allowed and some blocks have saved their content with HTML included, and afterwards the administrator changes the setting to &amp;quot;off&amp;quot;, this will only prevent subsequent content changes from including HTML. Blocks which already had HTML in their content would continue to display it!&lt;br /&gt;
&lt;br /&gt;
Following that train of thought, the next stop is realizing that we wouldn&#039;t have this problem if we had chosen the lazy approach a while back, because in that case we would &amp;quot;sanitize&amp;quot; each block&#039;s content just before it was displayed. &lt;br /&gt;
&lt;br /&gt;
The only thing we can do with the eager approach is strip all the tags from the content of all SimpleHTML instances as soon as the admin setting is changed to &amp;quot;HTML off&amp;quot;; but even then, turning the setting back to &amp;quot;HTML on&amp;quot; won&#039;t bring back the tags we stripped away. On the other hand, the lazy approach might be slower, but it&#039;s more versatile; we can choose whether to strip or keep the HTML before displaying the content, and we won&#039;t lose it at all if the admin toggles the setting off and on again. Isn&#039;t the life of a developer simple and wonderful?&lt;br /&gt;
&lt;br /&gt;
=== Exercise === &lt;br /&gt;
We will let this part of the tutorial come to a close with the obligatory exercise for the reader: &lt;br /&gt;
In order to have the SimpleHTML block work &amp;quot;correctly&amp;quot;, find out how to strengthen the eager approach to strip out all tags from the existing configuration of all instances of our block, &#039;&#039;&#039;or&#039;&#039;&#039; go back and implement the lazy approach instead. &lt;br /&gt;
(Hint: Do that in the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method.)&lt;br /&gt;
&lt;br /&gt;
=== UPDATING: === &lt;br /&gt;
Prior to version 1.5, the file &#039;&#039;config_global.html&#039;&#039; was named simply &#039;&#039;config.html&#039;&#039;. Also, the methods [[Blocks_Howto#method_config_save| config_save]] and [[Blocks_Howto#method_config_print| config_print]] were named &#039;&#039;&#039;handle_config&#039;&#039;&#039; and &#039;&#039;&#039;print_config&#039;&#039;&#039; respectively. Upgrading a block to work with Moodle 1.5 involves updating these aspects; refer to [[Blocks_Howto#appendix_b| Appendix B]] for more information.&lt;br /&gt;
&lt;br /&gt;
== Eye Candy ==&lt;br /&gt;
&lt;br /&gt;
Our block is just about complete functionally, so now let&#039;s take a look at some of the tricks we can use to make its behavior customized in a few more useful ways.&lt;br /&gt;
&lt;br /&gt;
First of all, there are a couple of ways we can adjust the visual aspects of our block. For starters, it might be useful to create a block that doesn&#039;t display a header (title) at all. You can see this effect in action in the Course Description block that comes with Moodle. This behavior is achieved by, you guessed it, adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function hide_header() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One more note here: we cannot just set an empty title inside the block&#039;s [[Development:Blocks/Appendix_A#init.28.29| init()]] method; it&#039;s necessary for each block to have a unique, non-empty title after [[Development:Blocks/Appendix_A#init.28.29| init()]] is called so that Moodle can use those titles to differentiate between all of the installed blocks.&lt;br /&gt;
&lt;br /&gt;
Another adjustment we might want to do is instruct our block to take up a certain amount of width on screen. Moodle handles this as a two-part process: first, it queries each block about its preferred width and takes the maximum number as the desired value. Then, the page that&#039;s being displayed can choose to use this value or, more probably, bring it within some specific range of values if it isn&#039;t already. That means that the width setting is a best-effort settlement; your block can &#039;&#039;request&#039;&#039; a certain width and Moodle will &#039;&#039;try&#039;&#039; to provide it, but there&#039;s no guarantee whatsoever about the end result. As a concrete example, all standard Moodle course formats will deliver any requested width between 180 and 210 pixels, inclusive.&lt;br /&gt;
&lt;br /&gt;
To instruct Moodle about our block&#039;s preferred width, we add one more method to the block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function preferred_width() {&lt;br /&gt;
  // The preferred value is in pixels&lt;br /&gt;
  return 200;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This will make our block (and all the other blocks displayed at the same side of the page) a bit wider than standard.&lt;br /&gt;
&lt;br /&gt;
Finally, we can also affect some properties of the actual HTML that will be used to print our block. Each block is fully contained within a &amp;amp;lt;table&amp;amp;gt; element, inside which all the HTML for that block is printed. We can instruct Moodle to add HTML attributes with specific values to that container. This would be done to either a) directly affect the end result (if we say, assign bgcolor=&amp;quot;black&amp;quot;), or b) give us freedom to customize the end result using CSS (this is in fact done by default as we &#039;ll see below).&lt;br /&gt;
&lt;br /&gt;
The default behavior of this feature in our case will assign to our block&#039;s container the class HTML attribute with the value &amp;quot;sideblock block_simplehtml&amp;quot; (the prefix &amp;quot;block_&amp;quot; followed by the name of our block, lowercased). We can then use that class to make CSS selectors in our theme to alter this block&#039;s visual style (for example, &amp;quot;.sideblock.block_simplehtml { border: 1px black solid}&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To change the default behavior, we will need to define a method which returns an associative array of attribute names and values. For example, the version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
  return array(&lt;br /&gt;
    &#039;class&#039;       =&amp;gt; &#039;sideblock block_&#039;. $this-&amp;gt;name(),&lt;br /&gt;
    &#039;onmouseover&#039; =&amp;gt; &amp;quot;alert(&#039;Mouseover on our block!&#039;);&amp;quot;&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will result in a mouseover event being added to our block using JavaScript, just as if we had written the onmouseover=&amp;quot;alert(...)&amp;quot; part ourselves in HTML. Note that we actually duplicate the part which sets the class attribute (we want to keep that, and since we override the default behavior it&#039;s our responsibility to emulate it if required). &lt;br /&gt;
&lt;br /&gt;
And the final elegant touch is that we don&#039;t set the class to the hard-coded value &amp;quot;block_simplehtml&amp;quot; but instead use the [[Development:Blocks/Appendix_A#name.28.29| name()]] method to make it dynamically match our block&#039;s name.&lt;br /&gt;
&lt;br /&gt;
== Authorized Personnel Only ==&lt;br /&gt;
&lt;br /&gt;
It&#039;s not difficult to imagine a block which is very useful in some circumstances but it simply cannot be made meaningful in others. An example of this would be the &amp;quot;Social Activities&amp;quot; block which is indeed useful in a course with the social format, but doesn&#039;t do anything useful in a course with the weeks format. There should be some way of allowing the use of such blocks only where they are indeed meaningful, and not letting them confuse users if they are not.&lt;br /&gt;
&lt;br /&gt;
Moodle allows us to declare which course formats each block is allowed to be displayed in, and enforces these restrictions as set by the block developers at all times. The information is given to Moodle as a standard associative array, with each key corresponding to a page format and defining a boolean value (true/false) that declares whether the block should be allowed to appear in that page format.&lt;br /&gt;
&lt;br /&gt;
Notice the deliberate use of the term &#039;&#039;page&#039;&#039; instead of &#039;&#039;course&#039;&#039; in the above paragraph. This is because in Moodle 1.5 and onwards, blocks can be displayed in any page that supports them. The best example of such pages are the course pages, but we are not restricted to them. For instance, the quiz view page (the first one we see when we click on the name of the quiz) also supports blocks.&lt;br /&gt;
&lt;br /&gt;
The format names we can use for the pages derive from the name of the script which is actually used to display that page. For example, when we are looking at a course, the script is &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/course/view.php&amp;lt;/span&amp;gt; (this is evident from the browser&#039;s address line). Thus, the format name of that page is &#039;&#039;&#039;course-view&#039;&#039;&#039;. It follows easily that the format name for a quiz view page is &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039;. This rule of thumb does have a few exceptions, however:&lt;br /&gt;
&lt;br /&gt;
# The format name for the front page of Moodle is &#039;&#039;&#039;site-index&#039;&#039;&#039;.&lt;br /&gt;
# The format name for courses is actually not just &#039;&#039;&#039;course-view&#039;&#039;&#039;&amp;lt;nowiki&amp;gt;; it is &amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;course-view-weeks&#039;&#039;&#039;, &#039;&#039;&#039;course-view-topics&#039;&#039;&#039;, etc.&lt;br /&gt;
# Even though there is no such page, the format name &#039;&#039;&#039;all&#039;&#039;&#039; can be used as a catch-all option.&lt;br /&gt;
&lt;br /&gt;
We can include as many format names as we want in our definition of the applicable formats. Each format can be allowed or disallowed, and there are also three more rules that help resolve the question &amp;quot;is this block allowed into this page or not?&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
# Prefixes of a format name will match that format name; for example, &#039;&#039;&#039;mod&#039;&#039;&#039; will match all the activity modules. &#039;&#039;&#039;course-view&#039;&#039;&#039; will match any course, regardless of the course format. And finally, &#039;&#039;&#039;site&#039;&#039;&#039; will also match the front page (remember that its full format name is &#039;&#039;&#039;site-index&#039;&#039;&#039;).&lt;br /&gt;
# The more specialized a format name that matches our page is, the higher precedence it has when deciding if the block will be allowed. For example, &#039;&#039;&#039;mod&#039;&#039;&#039;, &#039;&#039;&#039;mod-quiz&#039;&#039;&#039; and &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; all match the quiz view page. But if all three are present, &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; will take precedence over the other two because it is a better match.&lt;br /&gt;
# The character &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; can be used in place of any word. For example, &#039;&#039;&#039;mod&#039;&#039;&#039; and &#039;&#039;&#039;mod-*&#039;&#039;&#039; are equivalent. At the time of this document&#039;s writing, there is no actual reason to utilize this &amp;quot;wildcard matching&amp;quot; feature, but it exists for future usage.&lt;br /&gt;
# The order that the format names appear does not make any difference.&lt;br /&gt;
All of the above are enough to make the situation sound complex, so let&#039;s look at some specific examples. First of all, to have our block appear &#039;&#039;&#039;only&#039;&#039;&#039; in the site front page, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&#039;site&#039; =&amp;gt; true);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Since &#039;&#039;&#039;all&#039;&#039;&#039; is missing, the block is disallowed from appearing in &#039;&#039;any&#039;&#039; course format; but then &#039;&#039;&#039;site&#039;&#039;&#039; is set to TRUE, so it&#039;s explicitly allowed to appear in the site front page (remember that &#039;&#039;&#039;site&#039;&#039;&#039; matches &#039;&#039;&#039;site-index&#039;&#039;&#039; because it&#039;s a prefix).&lt;br /&gt;
&lt;br /&gt;
For another example, if we wanted to allow the block to appear in all course formats &#039;&#039;except&#039;&#039; social, and also to &#039;&#039;not&#039;&#039; be allowed anywhere but in courses, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;course-view&#039; =&amp;gt; true, &lt;br /&gt;
    &#039;course-view-social&#039; =&amp;gt; false);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This time, we first allow the block to appear in all courses and then we explicitly disallow the social format.&lt;br /&gt;
For our final, most complicated example, suppose that a block can be displayed in the site front page, in courses (but not social courses) and also when we are viewing any activity module, &#039;&#039;except&#039;&#039; quiz. This would be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;site-index&#039; =&amp;gt; true,&lt;br /&gt;
          &#039;course-view&#039; =&amp;gt; true, &lt;br /&gt;
   &#039;course-view-social&#039; =&amp;gt; false,&lt;br /&gt;
                  &#039;mod&#039; =&amp;gt; true, &lt;br /&gt;
             &#039;mod-quiz&#039; =&amp;gt; false&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is not difficult to realize that the above accomplishes the objective if we remember that there is a &amp;quot;best match&amp;quot; policy to determine the end result.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039; &amp;lt;br /&amp;gt;&lt;br /&gt;
Prior to version 1.5, blocks were only allowed in courses (and in Moodle 1.4, in the site front page). Also, the keywords used to describe the valid course formats at the time were slightly different and had to be changed in order to allow for a more open architecture. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== Lists and Icons ==&lt;br /&gt;
&lt;br /&gt;
In this final part of the guide we will briefly discuss an additional capability of Moodle&#039;s block system, namely the ability to very easily create blocks that display a list of choices to the user. This list is displayed with one item per line, and an optional image (icon) next to the item. An example of such a &#039;&#039;list block&#039;&#039; is the standard Moodle &amp;quot;admin&amp;quot; block, which illustrates all the points discussed in this section.&lt;br /&gt;
&lt;br /&gt;
As we have seen so far, blocks use two properties of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]]: &amp;quot;text&amp;quot; and &amp;quot;footer&amp;quot;. The text is displayed as-is as the block content, and the footer is displayed below the content in a smaller font size. List blocks use $this-&amp;gt;content-&amp;gt;footer in the exact same way, but they ignore $this-&amp;gt;content-&amp;gt;text.&lt;br /&gt;
&lt;br /&gt;
Instead, Moodle expects such blocks to set two other properties when the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called: $this-&amp;gt;content-&amp;gt;items and $this-&amp;gt;content-&amp;gt;icons. $this-&amp;gt;content-&amp;gt;items should be a numerically indexed array containing elements that represent the HTML for each item in the list that is going to be displayed. Usually these items will be HTML anchor tags which provide links to some page. $this-&amp;gt;content-&amp;gt;icons should also be a numerically indexed array, with exactly as many items as $this-&amp;gt;content-&amp;gt;items has. Each of these items should be a fully qualified HTML &amp;lt;img&amp;gt; tag, with &amp;quot;src&amp;quot;, &amp;quot;height&amp;quot;, &amp;quot;width&amp;quot; and &amp;quot;alt&amp;quot; attributes. Obviously, it makes sense to keep the images small and of a uniform size.&lt;br /&gt;
&lt;br /&gt;
In order to tell Moodle that we want to have a list block instead of the standard text block, we need to make a small change to our block class declaration. Instead of extending class &#039;&#039;&#039;block_base&#039;&#039;&#039;, our block will extend class &#039;&#039;&#039;block_list&#039;&#039;&#039;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
 class block_my_menu extends block_list {&lt;br /&gt;
     // The init() method does not need to change at all&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to making this change, we must of course also modify the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method to construct the [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] variable as discussed above:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function get_content() {&lt;br /&gt;
  if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content         = new stdClass;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items[] = &#039;&amp;lt;a href=&amp;quot;some_file.php&amp;quot;&amp;gt;Menu Option 1&amp;lt;/a&amp;gt;&#039;;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons[] = &#039;&amp;lt;img src=&amp;quot;images/icons/1.gif&amp;quot; class=&amp;quot;icon&amp;quot; alt=&amp;quot;&amp;quot; /&amp;gt;&#039;;&lt;br /&gt;
 &lt;br /&gt;
  // Add more list items here&lt;br /&gt;
 &lt;br /&gt;
  return $this-&amp;gt;content;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To summarize, if we want to create a list block instead of a text block, we just need to change the block class declaration and the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method. Adding the mandatory [[Development:Blocks/Appendix_A#init.28.29| init()]] method as discussed earlier will then give us our first list block in no time!&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== Appendices ==&lt;br /&gt;
&lt;br /&gt;
The appendices have been moved to separate pages:&lt;br /&gt;
&lt;br /&gt;
* Appendix A: [[Development:Blocks/Appendix A|&#039;&#039;block_base&#039;&#039; Reference]] &lt;br /&gt;
* Appendix B: [[Development:Blocks/Appendix B|Differences in the Blocks API for Moodle Versions prior to 1.5]]&lt;br /&gt;
* Appendix C: [[Development:Blocks/Appendix C|Creating Database Tables for Blocks (prior to 1.7)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Blocks]]&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
&lt;br /&gt;
[[es:Desarrollo de bloques]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55116</id>
		<title>Broken/Blocks</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55116"/>
		<updated>2009-04-30T10:37:53Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Eye Candy */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; A Step-by-step Guide To Creating Blocks &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Original Author: Jon Papaioannou (pj@moodle.org)&lt;br /&gt;
&lt;br /&gt;
The present document serves as a guide to developers who want to create their own blocks for use in Moodle. It applies to the 1.5 development version of Moodle (and any newer) &#039;&#039;&#039;only&#039;&#039;&#039;, as the blocks subsystem was rewritten and expanded for the 1.5 release. However, you can also find it useful if you want to modify blocks written for Moodle 1.3 and 1.4 to work with the latest versions (look at [[Development:Blocks/Appendix_B| Appendix B]]).&lt;br /&gt;
&lt;br /&gt;
The guide is written as an interactive course which aims to develop a configurable, multi-purpose block that displays arbitrary HTML. It&#039;s targeted mainly at people with little experience with Moodle or programming in general and aims to show how easy it is to create new blocks for Moodle. A certain small amount of PHP programming knowledge is still required, though. &lt;br /&gt;
&lt;br /&gt;
Experienced developers and those who just want a reference text should refer to [[Development:Blocks/Appendix_A| Appendix A]] because the main guide has a rather low concentration of pure information in the text.&lt;br /&gt;
&lt;br /&gt;
== Basic Concepts ==&lt;br /&gt;
&lt;br /&gt;
Through this guide, we will be following the creation of an &amp;quot;HTML&amp;quot; block from scratch in order to demonstrate most of the block features at our disposal. Our block will be named &amp;quot;SimpleHTML&amp;quot;. This does not constrain us regarding the name of the actual directory on the server where the files for our block will be stored, but for consistency we will follow the practice of using the lowercased form &amp;quot;simplehtml&amp;quot; in any case where such a name is required. &lt;br /&gt;
&lt;br /&gt;
Whenever we refer to a file or directory name which contains &amp;quot;simplehtml&amp;quot;, it&#039;s important to remember that &#039;&#039;only&#039;&#039; the &amp;quot;simplehtml&amp;quot; part is up to us to change; the rest is standardized and essential for Moodle to work correctly.&lt;br /&gt;
&lt;br /&gt;
Whenever a file&#039;s path is mentioned in this guide, it will always start with a slash. This refers to the Moodle home directory; all files and directories will be referred to with respect to that directory.&lt;br /&gt;
&lt;br /&gt;
== Ready, Set, Go! ==&lt;br /&gt;
&lt;br /&gt;
To define a &amp;quot;block&amp;quot; in Moodle, in the most basic case we need to provide just one source code file. We start by creating the directory &#039;&#039;/blocks/simplehtml/&#039;&#039; and creating a file named &#039;&#039;/blocks/simplehtml/&#039;&#039;&#039;&#039;&#039;block_simplehtml.php&#039;&#039;&#039; which will hold our code. We then begin coding the block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class block_simplehtml extends block_base {&lt;br /&gt;
  function init() {&lt;br /&gt;
    $this-&amp;gt;title   = get_string(&#039;simplehtml&#039;, &#039;block_simplehtml&#039;);&lt;br /&gt;
    $this-&amp;gt;version = 2004111200;&lt;br /&gt;
  }&lt;br /&gt;
  // The PHP tag and the curly bracket for the class definition &lt;br /&gt;
  // will only be closed after there is another function added in the next section.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first line is our block class definition; it must be named exactly in the manner shown. Again, only the &amp;quot;simplehtml&amp;quot; part can (and indeed must) change; everything else is standardized.&lt;br /&gt;
&lt;br /&gt;
Our class is then given a small method: [[Development:Blocks/Appendix_A#init.28.29| init()]]. This is essential for all blocks, and its purpose is to set the two class member variables listed inside it. But what do these values actually mean? Here&#039;s a more detailed description.&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Etitle| $this-&amp;gt;title]] is the title displayed in the header of our block. We can set it to whatever we like; in this case it&#039;s set to read the actual title from a language file we are presumably distributing together with the block. I &#039;ll skip ahead a bit here and say that if you want your block to display &#039;&#039;&#039;no&#039;&#039;&#039; title at all, then you should set this to any descriptive value you want (but &#039;&#039;&#039;not&#039;&#039;&#039; make it an empty string). We will later see [[Development:Blocks#Eye_Candy| how to disable the title&#039;s display]].&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] is the version of our block. This actually would only make a difference if your block wanted to keep its own data in special tables in the database (i.e. for very complex blocks). In that case the version number is used exactly as it&#039;s used in activities; an upgrade script uses it to incrementally upgrade an &amp;quot;old&amp;quot; version of the block&#039;s data to the latest. We will outline this process further ahead, since blocks tend to be relatively simple and not hold their own private data. &lt;br /&gt;
&lt;br /&gt;
In our example, this is certainly the case so we just set [[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] to &#039;&#039;&#039;YYYYMMDD00&#039;&#039;&#039; and forget about it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039;&amp;lt;br /&amp;gt; &lt;br /&gt;
Prior to version 1.5, the basic structure of each block class was slightly different. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== I Just Hear Static ==&lt;br /&gt;
In order to get our block to actually display something on screen, we need to add one more method to our class (before the final closing brace in our file). The new code is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
  function get_content() {&lt;br /&gt;
    if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
      return $this-&amp;gt;content;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;content         =  new stdClass;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
}   // Here&#039;s the closing curly bracket for the class definition&lt;br /&gt;
    // and here&#039;s the closing PHP tag from the section above.&lt;br /&gt;
?&amp;gt;  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can&#039;t get any simpler than that, can it? Let&#039;s dissect this method to see what&#039;s going on...&lt;br /&gt;
&lt;br /&gt;
First of all, there is a check that returns the current value of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] if it&#039;s not NULL; otherwise we proceed with &amp;quot;computing&amp;quot; it. Since the computation is potentially a time-consuming operation and it &#039;&#039;&#039;will&#039;&#039;&#039; be called several times for each block (Moodle works that way internally), we take a precaution and include this time-saver.&lt;br /&gt;
Supposing the content had not been computed before (it was NULL), we then define it from scratch. The code speaks for itself there, so there isn&#039;t much to say. Just keep in mind that we can use HTML both in the text &#039;&#039;&#039;and&#039;&#039;&#039; in the footer, if we want to.&lt;br /&gt;
&lt;br /&gt;
At this point our block should be capable of being automatically installed in Moodle and added to courses; visit your administration page to install it (Click &amp;quot;Notifications&amp;quot; under the Site Administration Block) and after seeing it in action come back to continue our tutorial.&lt;br /&gt;
&lt;br /&gt;
== Configure That Out ==&lt;br /&gt;
&lt;br /&gt;
The current version of our block doesn&#039;t really do much; it just displays a fixed message, which is not very useful. What we &#039;d really like to do is allow the teachers to customize what goes into the block. This, in block-speak, is called &amp;quot;instance configuration&amp;quot;. So let&#039;s give our block some instance configuration...&lt;br /&gt;
First of all, we need to tell Moodle that we want it to provide instance-specific configuration amenities to our block. That&#039;s as simple as adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This small change is enough to make Moodle display an &amp;quot;Edit...&amp;quot; icon in our block&#039;s header when we turn editing mode on in any course. However, if you try to click on that icon you will be presented with a notice that complains about the block&#039;s configuration not being implemented correctly. Try it, it&#039;s harmless.&lt;br /&gt;
Moodle&#039;s complaints do make sense. We told it that we want to have configuration, but we didn&#039;t say &#039;&#039;what&#039;&#039; kind of configuration we want, or how it should be displayed. To do that, we need to create one more file: &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/&#039;&#039;&#039;config_instance.html&#039;&#039;&#039;&amp;lt;/span&amp;gt; (which has to be named exactly like that). For the moment, copy paste the following into it and save:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;table cellpadding=&amp;quot;9&amp;quot; cellspacing=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_string(&#039;configcontent&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_textarea(true, 10, 50, 0, 0, &#039;text&#039;, $this-&amp;gt;config-&amp;gt;text); ?&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td colspan=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;) ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php use_html_editor(); ?&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It isn&#039;t difficult to see that the above code just provides us with a wysiwyg-editor-enabled textarea to write our block&#039;s desired content in and a submit button to save. But... what&#039;s $this-&amp;gt;config-&amp;gt;text? Well...&lt;br /&gt;
Moodle goes a long way to make things easier for block developers. Did you notice that the textarea is actually named &amp;quot;text&amp;quot;? When the submit button is pressed, Moodle saves each and every field it can find in our &#039;&#039;&#039;config_instance.html&#039;&#039;&#039; file as instance configuration data. &lt;br /&gt;
&lt;br /&gt;
We can then access that data as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;&#039;&#039;variablename&#039;&#039;&#039;&#039;&#039;, where &#039;&#039;variablename&#039;&#039; is the actual name we used for our field; in this case, &amp;quot;text&amp;quot;. So in essence, the above form just pre-populates the textarea with the current content of the block (as indeed it should) and then allows us to change it.&lt;br /&gt;
&lt;br /&gt;
You also might be surprised by the presence of a submit button and the absence of any &amp;lt;form&amp;gt; element at the same time. But the truth is, we don&#039;t need to worry about that at all; Moodle goes a really long way to make things easier for developers! We just print the configuration options we want, in any format we want; include a submit button, and Moodle will handle all the rest itself. The instance configuration variables are automatically at our disposal to access from any of the class methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29| init()]].&lt;br /&gt;
&lt;br /&gt;
In the event where the default behavior is not satisfactory, we can still override it. However, this requires advanced modifications to our block class and will not be covered here; refer to [[Development:Blocks/Appendix_A| Appendix A]] for more details.&lt;br /&gt;
Having now the ability to refer to this instance configuration data through [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]], the final twist is to tell our block to actually &#039;&#039;display&#039;&#039; what is saved in its configuration data. To do that, find this snippet in &#039;&#039;/blocks/simplehtml/block_simplehtml.php&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and change it to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Oh, and since the footer isn&#039;t really exciting at this point, we remove it from our block because it doesn&#039;t contribute anything. We could just as easily have decided to make the footer configurable in the above way, too. So for our latest code, the snippet becomes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After this discussion, our block is ready for prime time! Indeed, if you now visit any course with a SimpleHTML block, you will see that modifying its contents is now a snap.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Specialists ==&lt;br /&gt;
&lt;br /&gt;
Implementing instance configuration for the block&#039;s contents was good enough to whet our appetite, but who wants to stop there? Why not customize the block&#039;s title, too?&lt;br /&gt;
&lt;br /&gt;
Why not, indeed. Well, our first attempt to achieve this is natural enough: let&#039;s add another field to &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_instance.html&amp;lt;/span&amp;gt;. Here goes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&amp;lt;p&amp;gt;&lt;br /&gt;
    &amp;lt;?php print_string(&#039;configtitle&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;title&amp;quot; size=&amp;quot;30&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;config-&amp;gt;title; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We save the edited file, go to a course, edit the title of the block and... nothing happens! The instance configuration is saved correctly, all right (editing it once more proves that) but it&#039;s not being displayed. All we get is just the simple &amp;quot;SimpleHTML&amp;quot; title.&lt;br /&gt;
&lt;br /&gt;
That&#039;s not too weird, if we think back a bit. Do you remember that [[Development:Blocks/Appendix_A#init.28.29|init()]] method, where we set [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]]? We didn&#039;t actually change its value from then, and [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] is definitely not the same as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;title&#039;&#039;&#039; (to Moodle, at least). What we need is a way to update [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] with the value in the instance configuration. But as we said a bit earlier, we can use [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]] in all methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29|init()]]! So what can we do?&lt;br /&gt;
&lt;br /&gt;
Let&#039;s pull out another ace from our sleeve, and add this small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function specialization() {&lt;br /&gt;
  if(!empty($this-&amp;gt;config-&amp;gt;title)){&lt;br /&gt;
    $this-&amp;gt;title = $this-&amp;gt;config-&amp;gt;title;&lt;br /&gt;
  }else{&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;title = &#039;Some title ...&#039;;&lt;br /&gt;
  }&lt;br /&gt;
  if(empty($this-&amp;gt;config-&amp;gt;text)){&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;text = &#039;Some text ...&#039;;&lt;br /&gt;
  }    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Aha, here&#039;s what we wanted to do all along! But what&#039;s going on with the [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method?&lt;br /&gt;
&lt;br /&gt;
This &amp;quot;magic&amp;quot; method has actually a very nice property: it&#039;s &#039;&#039;guaranteed&#039;&#039; to be automatically called by Moodle as soon as our instance configuration is loaded and available (that is, immediately after [[Development:Blocks/Appendix_A#init.28.29|init()]] is called). That means before the block&#039;s content is computed for the first time, and indeed before &#039;&#039;anything&#039;&#039; else is done with the block. Thus, providing a [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method is the natural choice for any configuration data that needs to be acted upon &amp;quot;as soon as possible&amp;quot;, as in this case.&lt;br /&gt;
&lt;br /&gt;
== Now You See Me, Now You Don&#039;t ==&lt;br /&gt;
&lt;br /&gt;
Now would be a good time to mention another nifty technique that can be used in blocks, and which comes in handy quite often. Specifically, it may be the case that our block will have something interesting to display some of the time; but in some other cases, it won&#039;t have anything useful to say. (An example here would be the &amp;quot;Recent Activity&amp;quot; block, in the case where no recent activity in fact exists. &lt;br /&gt;
&lt;br /&gt;
However in that case the block chooses to explicitly inform you of the lack of said activity, which is arguably useful). It would be nice, then, to be able to have our block &amp;quot;disappear&amp;quot; if it&#039;s not needed to display it.&lt;br /&gt;
&lt;br /&gt;
This is indeed possible, and the way to do it is to make sure that after the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called, the block is completely void of content. Specifically, &amp;quot;void of content&amp;quot; means that both $this-&amp;gt;content-&amp;gt;text and $this-&amp;gt;content-&amp;gt;footer are each equal to the empty string (&amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt;). Moodle performs this check by calling the block&#039;s [[Development:Blocks/Appendix_A#is_empty.28.29| is_empty()]] method, and if the block is indeed empty then it is not displayed at all.&lt;br /&gt;
&lt;br /&gt;
Note that the exact value of the block&#039;s title and the presence or absence of a [[Development:Blocks/Appendix_A#hide_header.28.29| hide_header()]] method do &#039;&#039;not&#039;&#039; affect this behavior. A block is considered empty if it has no content, irrespective of anything else.&lt;br /&gt;
&lt;br /&gt;
== We Are Legion ==&lt;br /&gt;
&lt;br /&gt;
Right now our block is fully configurable, both in title and content. It&#039;s so versatile, in fact, that we could make pretty much anything out of it. It would be really nice to be able to add multiple blocks of this type to a single course. And, as you might have guessed, doing that is as simple as adding another small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_multiple() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Moodle that it should allow any number of instances of the SimpleHTML block in any course. After saving the changes to our file, Moodle immediately allows us to add multiple copies of the block without further ado!&lt;br /&gt;
&lt;br /&gt;
There are a couple more of interesting points to note here. First of all, even if a block itself allows multiple instances in the same page, the administrator still has the option of disallowing such behavior. This setting can be set separately for each block from the Administration / Configuration / Blocks page.&lt;br /&gt;
&lt;br /&gt;
And finally, a nice detail is that as soon as we defined an [[Development:Blocks/Appendix_A#instance_allow_multiple.28.29| instance_allow_multiple()]] method, the method [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] that was already defined became obsolete. &lt;br /&gt;
&lt;br /&gt;
Moodle assumes that if a block allows multiple instances of itself, those instances will want to be configured (what is the point of same multiple instances in the same page if they are identical?) and thus automatically provides an &amp;quot;Edit&amp;quot; icon. So, we can also remove the whole [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] method now without harm. We had only needed it when multiple instances of the block were not allowed.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Effects of Globalization ==&lt;br /&gt;
&lt;br /&gt;
Configuring each block instance with its own personal data is cool enough, but sometimes administrators need some way to &amp;quot;touch&amp;quot; all instances of a specific block at the same time. In the case of our SimpleHTML block, a few settings that would make sense to apply to all instances aren&#039;t that hard to come up with. &lt;br /&gt;
&lt;br /&gt;
For example, we might want to limit the contents of each block to only so many characters, or we might have a setting that filters HTML out of the block&#039;s contents, only allowing pure text in. Granted, such a feature wouldn&#039;t win us any awards for naming our block &amp;quot;SimpleHTML&amp;quot; but some tormented administrator somewhere might actually find it useful.&lt;br /&gt;
&lt;br /&gt;
This kind of configuration is called &amp;quot;global configuration&amp;quot; and applies only to a specific block type (all instances of that block type are affected, however). Implementing such configuration for our block is quite similar to implementing the instance configuration. We will now see how to implement the second example, having a setting that only allows text and not HTML in the block&#039;s contents.&lt;br /&gt;
First of all, we need to tell Moodle that we want our block to provide global configuration by, what a surprise, adding a small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function has_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we need to create a HTML file that actually prints out the configuration screen. In our case, we &#039;ll just print out a checkbox saying &amp;quot;Do not allow HTML in the content&amp;quot; and a &amp;quot;submit&amp;quot; button. Let&#039;s create the file &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_global.html&amp;lt;/span&amp;gt; which again must be named just so, and copy paste the following into it:&lt;br /&gt;
&lt;br /&gt;
[[Development_talk:Blocks|TODO: New settings.php method]] &lt;br /&gt;
: Just to note that general documentation about admin settings is at [[Development:Admin_settings#Individual_settings]]. In the absence of documentation, you can look at blocks/course_list, blocks/online_users and blocks/rss_client. They all use a settings.php file.--[[User:Tim Hunt|Tim Hunt]] 19:38, 28 January 2009 (CST)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;1&amp;quot;&lt;br /&gt;
   &amp;lt;?php if(!empty($CFG-&amp;gt;block_simplehtml_strict)) &lt;br /&gt;
             echo &#039;checked=&amp;quot;checked&amp;quot;&#039;; ?&amp;gt; /&amp;gt;&lt;br /&gt;
   &amp;lt;?php print_string(&#039;donotallowhtml&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;&lt;br /&gt;
 &amp;lt;p&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;); ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
True to our block&#039;s name, this looks simple enough. What it does is that it displays a checkbox named &amp;quot;block_simplehtml_strict&amp;quot; and if the Moodle configuration variable with the same name (i.e., $CFG-&amp;gt;block_simplehtml_strict) is set and not empty (that means it&#039;s not equal to an empty string, to zero, or to boolean FALSE) it displays the box as pre-checked (reflecting the current status). &lt;br /&gt;
&lt;br /&gt;
Why does it check the configuration setting with the same name? Because the default implementation of the global configuration saving code takes all the variables we have in our form and saves them as Moodle configuration options with the same name. Thus, it&#039;s good practice to use a descriptive name and also one that won&#039;t possibly conflict with the name of another setting. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;block_simplehtml_strict&amp;quot; clearly satisfies both requirements.&lt;br /&gt;
&lt;br /&gt;
The astute reader may have noticed that we actually have &#039;&#039;two&#039;&#039; input fields named &amp;quot;block_simplehtml_strict&amp;quot; in our configuration file. One is hidden and its value is always 0; the other is the checkbox and its value is 1. What gives? Why have them both there?&lt;br /&gt;
&lt;br /&gt;
Actually, this is a small trick we use to make our job as simple as possible. HTML forms work this way: if a checkbox in a form is not checked, its name does not appear at all in the variables passed to PHP when the form is submitted. That effectively means that, when we uncheck the box and click submit, the variable is not passed to PHP at all. Thus, PHP does not know to update its value to &amp;quot;0&amp;quot;, and our &amp;quot;strict&amp;quot; setting cannot be turned off at all once we turn it on for the first time. Not the behavior we want, surely.&lt;br /&gt;
&lt;br /&gt;
However, when PHP handles received variables from a form, the variables are processed in the order in which they appear in the form. If a variable comes up having the same name with an already-processed variable, the new value overwrites the old one. Taking advantage of this, our logic runs as follows: the variable &amp;quot;block_simplehtml_strict&amp;quot; is first unconditionally set to &amp;quot;0&amp;quot;. Then, &#039;&#039;if&#039;&#039; the box is checked, it is set to &amp;quot;1&amp;quot;, overwriting the previous value as discussed. The net result is that our configuration setting behaves as it should.&lt;br /&gt;
&lt;br /&gt;
To round our bag of tricks up, notice that the use of &#039;&#039;if(!empty($CFG-&amp;gt;block_simplehtml_strict))&#039;&#039; in the test for &amp;quot;should the box be checked by default?&amp;quot; is quite deliberate. The first time this script runs, the variable &#039;&#039;&#039;$CFG-&amp;gt;block_simplehtml_strict&#039;&#039;&#039; will not exist at all. After it&#039;s set for the first time, its value can be either &amp;quot;0&amp;quot; or &amp;quot;1&amp;quot;. Given that both &amp;quot;not set&amp;quot; and the string &amp;quot;0&amp;quot; evaluate as empty while the sting &amp;quot;1&amp;quot; does not, we manage to avoid any warnings from PHP regarding the variable not being set at all, &#039;&#039;and&#039;&#039; have a nice human-readable representation for its two possible values (&amp;quot;0&amp;quot; and &amp;quot;1&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
=== config_save() ===&lt;br /&gt;
&lt;br /&gt;
Now that we have managed to cram a respectable amount of tricks into a few lines of HTML, we might as well discuss the alternative in case that tricks are not enough for a specific configuration setup we have in mind. Saving the data is done in the method [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], the default implementation of which is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  // Default behavior: save all variables as $CFG properties&lt;br /&gt;
  foreach ($data as $name =&amp;gt; $value) {&lt;br /&gt;
    set_config($name, $value);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As can be clearly seen, Moodle passes this method an associative array $data which contains all the variables coming in from our configuration screen. If we wanted to do the job without the &amp;quot;hidden variable with the same name&amp;quot; trick we used above, one way to do it would be by overriding this method with the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  if(isset($data[&#039;block_simplehtml_strict&#039;])) {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;1&#039;);&lt;br /&gt;
  }else {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;0&#039;);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quite straightfoward: if the variable &amp;quot;block_simplehtml_strict&amp;quot; is passed to us, then it can only mean that the user has checked it, so set the configuration variable with the same name to &amp;quot;1&amp;quot;. Otherwise, set it to &amp;quot;0&amp;quot;. Of course, this version would need to be updated if we add more configuration options because it doesn&#039;t respond to them as the default implementation does. Still, it&#039;s useful to know how we can override the default implementation if it does not fit our needs (for example, we might not want to save the variable as part of the Moodle configuration but do something else with it).&lt;br /&gt;
&lt;br /&gt;
So, we are now at the point where we know if the block should allow HTML tags in its content or not. How do we get the block to actually respect that setting?&lt;br /&gt;
&lt;br /&gt;
We could decide to do one of two things: either have the block &amp;quot;clean&amp;quot; HTML out from the input before saving it in the instance configuration and then display it as-is (the &amp;quot;eager&amp;quot; approach); or have it save the data &amp;quot;as is&amp;quot; and then clean it up each time just before displaying it (the &amp;quot;lazy&amp;quot; approach). The eager approach involves doing work once when saving the configuration; the lazy approach means doing work each time the block is displayed and thus it promises to be worse performance-wise. We shall hence go with the eager approach.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
=== instance_config_save() ===&lt;br /&gt;
&lt;br /&gt;
Much as we did just before with overriding [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], what is needed here is overriding the method [[Development:Blocks/Appendix_A#instance_config_save.28.29| instance_config_save()]] which handles the instance configuration. The default implementation is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  $data = stripslashes_recursive($data);&lt;br /&gt;
  $this-&amp;gt;config = $data;&lt;br /&gt;
  return set_field(&#039;block_instance&#039;, &lt;br /&gt;
                   &#039;configdata&#039;,&lt;br /&gt;
                    base64_encode(serialize($data)),&lt;br /&gt;
                   &#039;id&#039;, &lt;br /&gt;
                   $this-&amp;gt;instance-&amp;gt;id);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This may look intimidating at first (what&#039;s all this stripslashes_recursive() and base64_encode() and serialize() stuff?) but do not despair; we won&#039;t have to touch any of it. We will only add some extra validation code in the beginning and then instruct Moodle to additionally call this default implementation to do the actual storing of the data. Specifically, we will add a method to our class which goes like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  // Clean the data if we have to&lt;br /&gt;
  global $CFG;&lt;br /&gt;
  if(!empty($CFG-&amp;gt;block_simplehtml_strict)) {&lt;br /&gt;
    $data-&amp;gt;text = strip_tags($data-&amp;gt;text);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // And now forward to the default implementation defined in the parent class&lt;br /&gt;
  return parent::instance_config_save($data);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At last! Now the administrator has absolute power of life and death over what type of content is allowed in our &amp;quot;SimpleHTML&amp;quot; block! Absolute? Well... not exactly. In fact, if we think about it for a while, it will become apparent that if at some point in time HTML is allowed and some blocks have saved their content with HTML included, and afterwards the administrator changes the setting to &amp;quot;off&amp;quot;, this will only prevent subsequent content changes from including HTML. Blocks which already had HTML in their content would continue to display it!&lt;br /&gt;
&lt;br /&gt;
Following that train of thought, the next stop is realizing that we wouldn&#039;t have this problem if we had chosen the lazy approach a while back, because in that case we would &amp;quot;sanitize&amp;quot; each block&#039;s content just before it was displayed. &lt;br /&gt;
&lt;br /&gt;
The only thing we can do with the eager approach is strip all the tags from the content of all SimpleHTML instances as soon as the admin setting is changed to &amp;quot;HTML off&amp;quot;; but even then, turning the setting back to &amp;quot;HTML on&amp;quot; won&#039;t bring back the tags we stripped away. On the other hand, the lazy approach might be slower, but it&#039;s more versatile; we can choose whether to strip or keep the HTML before displaying the content, and we won&#039;t lose it at all if the admin toggles the setting off and on again. Isn&#039;t the life of a developer simple and wonderful?&lt;br /&gt;
&lt;br /&gt;
=== Exercise === &lt;br /&gt;
We will let this part of the tutorial come to a close with the obligatory exercise for the reader: &lt;br /&gt;
In order to have the SimpleHTML block work &amp;quot;correctly&amp;quot;, find out how to strengthen the eager approach to strip out all tags from the existing configuration of all instances of our block, &#039;&#039;&#039;or&#039;&#039;&#039; go back and implement the lazy approach instead. &lt;br /&gt;
(Hint: Do that in the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method.)&lt;br /&gt;
&lt;br /&gt;
=== UPDATING: === &lt;br /&gt;
Prior to version 1.5, the file &#039;&#039;config_global.html&#039;&#039; was named simply &#039;&#039;config.html&#039;&#039;. Also, the methods [[Blocks_Howto#method_config_save| config_save]] and [[Blocks_Howto#method_config_print| config_print]] were named &#039;&#039;&#039;handle_config&#039;&#039;&#039; and &#039;&#039;&#039;print_config&#039;&#039;&#039; respectively. Upgrading a block to work with Moodle 1.5 involves updating these aspects; refer to [[Blocks_Howto#appendix_b| Appendix B]] for more information.&lt;br /&gt;
&lt;br /&gt;
== Eye Candy ==&lt;br /&gt;
&lt;br /&gt;
Our block is just about complete functionally, so now let&#039;s take a look at some of the tricks we can use to make its behavior customized in a few more useful ways.&lt;br /&gt;
&lt;br /&gt;
First of all, there are a couple of ways we can adjust the visual aspects of our block. For starters, it might be useful to create a block that doesn&#039;t display a header (title) at all. You can see this effect in action in the Course Description block that comes with Moodle. This behavior is achieved by, you guessed it, adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function hide_header() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One more note here: we cannot just set an empty title inside the block&#039;s [[Development:Blocks/Appendix_A#init.28.29| init()]] method; it&#039;s necessary for each block to have a unique, non-empty title after [[Development:Blocks/Appendix_A#init.28.29| init()]] is called so that Moodle can use those titles to differentiate between all of the installed blocks.&lt;br /&gt;
&lt;br /&gt;
Another adjustment we might want to do is instruct our block to take up a certain amount of width on screen. Moodle handles this as a two-part process: first, it queries each block about its preferred width and takes the maximum number as the desired value. Then, the page that&#039;s being displayed can choose to use this value or, more probably, bring it within some specific range of values if it isn&#039;t already. That means that the width setting is a best-effort settlement; your block can &#039;&#039;request&#039;&#039; a certain width and Moodle will &#039;&#039;try&#039;&#039; to provide it, but there&#039;s no guarantee whatsoever about the end result. As a concrete example, all standard Moodle course formats will deliver any requested width between 180 and 210 pixels, inclusive.&lt;br /&gt;
&lt;br /&gt;
To instruct Moodle about our block&#039;s preferred width, we add one more method to the block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function preferred_width() {&lt;br /&gt;
  // The preferred value is in pixels&lt;br /&gt;
  return 200;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This will make our block (and all the other blocks displayed at the same side of the page) a bit wider than standard.&lt;br /&gt;
&lt;br /&gt;
Finally, we can also affect some properties of the actual HTML that will be used to print our block. Each block is fully contained within a &amp;amp;lt;table&amp;amp;gt; element, inside which all the HTML for that block is printed. We can instruct Moodle to add HTML attributes with specific values to that container. This would be done to either a) directly affect the end result (if we say, assign bgcolor=&amp;quot;black&amp;quot;), or b) give us freedom to customize the end result using CSS (this is in fact done by default as we &#039;ll see below).&lt;br /&gt;
&lt;br /&gt;
The default behavior of this feature in our case will assign to our block&#039;s container the class HTML attribute with the value &amp;quot;sideblock block_simplehtml&amp;quot; (the prefix &amp;quot;block_&amp;quot; followed by the name of our block, lowercased). We can then use that class to make CSS selectors in our theme to alter this block&#039;s visual style (for example, &amp;quot;.sideblock.block_simplehtml { border: 1px black solid}&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To change the default behavior, we will need to define a method which returns an associative array of attribute names and values. For example, the version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
  return array(&lt;br /&gt;
    &#039;class&#039;       =&amp;gt; &#039;sideblock block_&#039;. $this-&amp;gt;name(),&lt;br /&gt;
    &#039;onmouseover&#039; =&amp;gt; &amp;quot;alert(&#039;Mouseover on our block!&#039;);&amp;quot;&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will result in a mouseover event being added to our block using JavaScript, just as if we had written the onmouseover=&amp;quot;alert(...)&amp;quot; part ourselves in HTML. Note that we actually duplicate the part which sets the class attribute (we want to keep that, and since we override the default behavior it&#039;s our responsibility to emulate it if required). &lt;br /&gt;
&lt;br /&gt;
And the final elegant touch is that we don&#039;t set the class to the hard-coded value &amp;quot;block_simplehtml&amp;quot; but instead use the [[Development:Blocks/Appendix_A#name.28.29| name()]] method to make it dynamically match our block&#039;s name.&lt;br /&gt;
&lt;br /&gt;
== Authorized Personnel Only ==&lt;br /&gt;
&lt;br /&gt;
It&#039;s not difficult to imagine a block which is very useful in some circumstances but it simply cannot be made meaningful in others. An example of this would be the &amp;quot;Social Activities&amp;quot; block which is indeed useful in a course with the social format, but doesn&#039;t do anything useful in a course with the weeks format. There should be some way of allowing the use of such blocks only where they are indeed meaningful, and not letting them confuse users if they are not.&lt;br /&gt;
&lt;br /&gt;
Moodle allows us to declare which course formats each block is allowed to be displayed in, and enforces these restrictions as set by the block developers at all times. The information is given to Moodle as a standard associative array, with each key corresponding to a page format and defining a boolean value (true/false) that declares whether the block should be allowed to appear in that page format.&lt;br /&gt;
&lt;br /&gt;
Notice the deliberate use of the term &#039;&#039;page&#039;&#039; instead of &#039;&#039;course&#039;&#039; in the above paragraph. This is because in Moodle 1.5 and onwards, blocks can be displayed in any page that supports them. The best example of such pages are the course pages, but we are not restricted to them. For instance, the quiz view page (the first one we see when we click on the name of the quiz) also supports blocks.&lt;br /&gt;
&lt;br /&gt;
The format names we can use for the pages derive from the name of the script which is actually used to display that page. For example, when we are looking at a course, the script is &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/course/view.php&amp;lt;/span&amp;gt; (this is evident from the browser&#039;s address line). Thus, the format name of that page is &#039;&#039;&#039;course-view&#039;&#039;&#039;. It follows easily that the format name for a quiz view page is &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039;. This rule of thumb does have a few exceptions, however:&lt;br /&gt;
&lt;br /&gt;
# The format name for the front page of Moodle is &#039;&#039;&#039;site-index&#039;&#039;&#039;.&lt;br /&gt;
# The format name for courses is actually not just &#039;&#039;&#039;course-view&#039;&#039;&#039;&amp;lt;nowiki&amp;gt;; it is &amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;course-view-weeks&#039;&#039;&#039;, &#039;&#039;&#039;course-view-topics&#039;&#039;&#039;, etc.&lt;br /&gt;
# Even though there is no such page, the format name &#039;&#039;&#039;all&#039;&#039;&#039; can be used as a catch-all option.&lt;br /&gt;
&lt;br /&gt;
We can include as many format names as we want in our definition of the applicable formats. Each format can be allowed or disallowed, and there are also three more rules that help resolve the question &amp;quot;is this block allowed into this page or not?&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
# Prefixes of a format name will match that format name; for example, &#039;&#039;&#039;mod&#039;&#039;&#039; will match all the activity modules. &#039;&#039;&#039;course-view&#039;&#039;&#039; will match any course, regardless of the course format. And finally, &#039;&#039;&#039;site&#039;&#039;&#039; will also match the front page (remember that its full format name is &#039;&#039;&#039;site-index&#039;&#039;&#039;).&lt;br /&gt;
# The more specialized a format name that matches our page is, the higher precedence it has when deciding if the block will be allowed. For example, &#039;&#039;&#039;mod&#039;&#039;&#039;, &#039;&#039;&#039;mod-quiz&#039;&#039;&#039; and &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; all match the quiz view page. But if all three are present, &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; will take precedence over the other two because it is a better match.&lt;br /&gt;
# The character &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; can be used in place of any word. For example, &#039;&#039;&#039;mod&#039;&#039;&#039; and &#039;&#039;&#039;mod-*&#039;&#039;&#039; are equivalent. At the time of this document&#039;s writing, there is no actual reason to utilize this &amp;quot;wildcard matching&amp;quot; feature, but it exists for future usage.&lt;br /&gt;
# The order that the format names appear does not make any difference.&lt;br /&gt;
All of the above are enough to make the situation sound complex, so let&#039;s look at some specific examples. First of all, to have our block appear &#039;&#039;&#039;only&#039;&#039;&#039; in the site front page, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&#039;site&#039; =&amp;gt; TRUE);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Since &#039;&#039;&#039;all&#039;&#039;&#039; is missing, the block is disallowed from appearing in &#039;&#039;any&#039;&#039; course format; but then &#039;&#039;&#039;site&#039;&#039;&#039; is set to TRUE, so it&#039;s explicitly allowed to appear in the site front page (remember that &#039;&#039;&#039;site&#039;&#039;&#039; matches &#039;&#039;&#039;site-index&#039;&#039;&#039; because it&#039;s a prefix).&lt;br /&gt;
&lt;br /&gt;
For another example, if we wanted to allow the block to appear in all course formats &#039;&#039;except&#039;&#039; social, and also to &#039;&#039;not&#039;&#039; be allowed anywhere but in courses, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;course-view&#039; =&amp;gt; TRUE, &lt;br /&gt;
    &#039;course-view-social&#039; =&amp;gt; FALSE);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This time, we first allow the block to appear in all courses and then we explicitly disallow the social format.&lt;br /&gt;
For our final, most complicated example, suppose that a block can be displayed in the site front page, in courses (but not social courses) and also when we are viewing any activity module, &#039;&#039;except&#039;&#039; quiz. This would be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;site-index&#039; =&amp;gt; TRUE,&lt;br /&gt;
          &#039;course-view&#039; =&amp;gt; TRUE, &lt;br /&gt;
   &#039;course-view-social&#039; =&amp;gt; FALSE,&lt;br /&gt;
                  &#039;mod&#039; =&amp;gt; TRUE, &lt;br /&gt;
             &#039;mod-quiz&#039; =&amp;gt; FALSE&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is not difficult to realize that the above accomplishes the objective if we remember that there is a &amp;quot;best match&amp;quot; policy to determine the end result.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039; &amp;lt;br /&amp;gt;&lt;br /&gt;
Prior to version 1.5, blocks were only allowed in courses (and in Moodle 1.4, in the site front page). Also, the keywords used to describe the valid course formats at the time were slightly different and had to be changed in order to allow for a more open architecture. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== Lists and Icons ==&lt;br /&gt;
&lt;br /&gt;
In this final part of the guide we will briefly discuss an additional capability of Moodle&#039;s block system, namely the ability to very easily create blocks that display a list of choices to the user. This list is displayed with one item per line, and an optional image (icon) next to the item. An example of such a &#039;&#039;list block&#039;&#039; is the standard Moodle &amp;quot;admin&amp;quot; block, which illustrates all the points discussed in this section.&lt;br /&gt;
&lt;br /&gt;
As we have seen so far, blocks use two properties of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]]: &amp;quot;text&amp;quot; and &amp;quot;footer&amp;quot;. The text is displayed as-is as the block content, and the footer is displayed below the content in a smaller font size. List blocks use $this-&amp;gt;content-&amp;gt;footer in the exact same way, but they ignore $this-&amp;gt;content-&amp;gt;text.&lt;br /&gt;
&lt;br /&gt;
Instead, Moodle expects such blocks to set two other properties when the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called: $this-&amp;gt;content-&amp;gt;items and $this-&amp;gt;content-&amp;gt;icons. $this-&amp;gt;content-&amp;gt;items should be a numerically indexed array containing elements that represent the HTML for each item in the list that is going to be displayed. Usually these items will be HTML anchor tags which provide links to some page. $this-&amp;gt;content-&amp;gt;icons should also be a numerically indexed array, with exactly as many items as $this-&amp;gt;content-&amp;gt;items has. Each of these items should be a fully qualified HTML &amp;lt;img&amp;gt; tag, with &amp;quot;src&amp;quot;, &amp;quot;height&amp;quot;, &amp;quot;width&amp;quot; and &amp;quot;alt&amp;quot; attributes. Obviously, it makes sense to keep the images small and of a uniform size.&lt;br /&gt;
&lt;br /&gt;
In order to tell Moodle that we want to have a list block instead of the standard text block, we need to make a small change to our block class declaration. Instead of extending class &#039;&#039;&#039;block_base&#039;&#039;&#039;, our block will extend class &#039;&#039;&#039;block_list&#039;&#039;&#039;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
 class block_my_menu extends block_list {&lt;br /&gt;
     // The init() method does not need to change at all&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to making this change, we must of course also modify the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method to construct the [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] variable as discussed above:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function get_content() {&lt;br /&gt;
  if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content         = new stdClass;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items[] = &#039;&amp;lt;a href=&amp;quot;some_file.php&amp;quot;&amp;gt;Menu Option 1&amp;lt;/a&amp;gt;&#039;;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons[] = &#039;&amp;lt;img src=&amp;quot;images/icons/1.gif&amp;quot; class=&amp;quot;icon&amp;quot; alt=&amp;quot;&amp;quot; /&amp;gt;&#039;;&lt;br /&gt;
 &lt;br /&gt;
  // Add more list items here&lt;br /&gt;
 &lt;br /&gt;
  return $this-&amp;gt;content;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To summarize, if we want to create a list block instead of a text block, we just need to change the block class declaration and the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method. Adding the mandatory [[Development:Blocks/Appendix_A#init.28.29| init()]] method as discussed earlier will then give us our first list block in no time!&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== Appendices ==&lt;br /&gt;
&lt;br /&gt;
The appendices have been moved to separate pages:&lt;br /&gt;
&lt;br /&gt;
* Appendix A: [[Development:Blocks/Appendix A|&#039;&#039;block_base&#039;&#039; Reference]] &lt;br /&gt;
* Appendix B: [[Development:Blocks/Appendix B|Differences in the Blocks API for Moodle Versions prior to 1.5]]&lt;br /&gt;
* Appendix C: [[Development:Blocks/Appendix C|Creating Database Tables for Blocks (prior to 1.7)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Blocks]]&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
&lt;br /&gt;
[[es:Desarrollo de bloques]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55114</id>
		<title>Broken/Blocks</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55114"/>
		<updated>2009-04-30T10:37:45Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* The Effects of Globalization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; A Step-by-step Guide To Creating Blocks &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Original Author: Jon Papaioannou (pj@moodle.org)&lt;br /&gt;
&lt;br /&gt;
The present document serves as a guide to developers who want to create their own blocks for use in Moodle. It applies to the 1.5 development version of Moodle (and any newer) &#039;&#039;&#039;only&#039;&#039;&#039;, as the blocks subsystem was rewritten and expanded for the 1.5 release. However, you can also find it useful if you want to modify blocks written for Moodle 1.3 and 1.4 to work with the latest versions (look at [[Development:Blocks/Appendix_B| Appendix B]]).&lt;br /&gt;
&lt;br /&gt;
The guide is written as an interactive course which aims to develop a configurable, multi-purpose block that displays arbitrary HTML. It&#039;s targeted mainly at people with little experience with Moodle or programming in general and aims to show how easy it is to create new blocks for Moodle. A certain small amount of PHP programming knowledge is still required, though. &lt;br /&gt;
&lt;br /&gt;
Experienced developers and those who just want a reference text should refer to [[Development:Blocks/Appendix_A| Appendix A]] because the main guide has a rather low concentration of pure information in the text.&lt;br /&gt;
&lt;br /&gt;
== Basic Concepts ==&lt;br /&gt;
&lt;br /&gt;
Through this guide, we will be following the creation of an &amp;quot;HTML&amp;quot; block from scratch in order to demonstrate most of the block features at our disposal. Our block will be named &amp;quot;SimpleHTML&amp;quot;. This does not constrain us regarding the name of the actual directory on the server where the files for our block will be stored, but for consistency we will follow the practice of using the lowercased form &amp;quot;simplehtml&amp;quot; in any case where such a name is required. &lt;br /&gt;
&lt;br /&gt;
Whenever we refer to a file or directory name which contains &amp;quot;simplehtml&amp;quot;, it&#039;s important to remember that &#039;&#039;only&#039;&#039; the &amp;quot;simplehtml&amp;quot; part is up to us to change; the rest is standardized and essential for Moodle to work correctly.&lt;br /&gt;
&lt;br /&gt;
Whenever a file&#039;s path is mentioned in this guide, it will always start with a slash. This refers to the Moodle home directory; all files and directories will be referred to with respect to that directory.&lt;br /&gt;
&lt;br /&gt;
== Ready, Set, Go! ==&lt;br /&gt;
&lt;br /&gt;
To define a &amp;quot;block&amp;quot; in Moodle, in the most basic case we need to provide just one source code file. We start by creating the directory &#039;&#039;/blocks/simplehtml/&#039;&#039; and creating a file named &#039;&#039;/blocks/simplehtml/&#039;&#039;&#039;&#039;&#039;block_simplehtml.php&#039;&#039;&#039; which will hold our code. We then begin coding the block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class block_simplehtml extends block_base {&lt;br /&gt;
  function init() {&lt;br /&gt;
    $this-&amp;gt;title   = get_string(&#039;simplehtml&#039;, &#039;block_simplehtml&#039;);&lt;br /&gt;
    $this-&amp;gt;version = 2004111200;&lt;br /&gt;
  }&lt;br /&gt;
  // The PHP tag and the curly bracket for the class definition &lt;br /&gt;
  // will only be closed after there is another function added in the next section.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first line is our block class definition; it must be named exactly in the manner shown. Again, only the &amp;quot;simplehtml&amp;quot; part can (and indeed must) change; everything else is standardized.&lt;br /&gt;
&lt;br /&gt;
Our class is then given a small method: [[Development:Blocks/Appendix_A#init.28.29| init()]]. This is essential for all blocks, and its purpose is to set the two class member variables listed inside it. But what do these values actually mean? Here&#039;s a more detailed description.&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Etitle| $this-&amp;gt;title]] is the title displayed in the header of our block. We can set it to whatever we like; in this case it&#039;s set to read the actual title from a language file we are presumably distributing together with the block. I &#039;ll skip ahead a bit here and say that if you want your block to display &#039;&#039;&#039;no&#039;&#039;&#039; title at all, then you should set this to any descriptive value you want (but &#039;&#039;&#039;not&#039;&#039;&#039; make it an empty string). We will later see [[Development:Blocks#Eye_Candy| how to disable the title&#039;s display]].&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] is the version of our block. This actually would only make a difference if your block wanted to keep its own data in special tables in the database (i.e. for very complex blocks). In that case the version number is used exactly as it&#039;s used in activities; an upgrade script uses it to incrementally upgrade an &amp;quot;old&amp;quot; version of the block&#039;s data to the latest. We will outline this process further ahead, since blocks tend to be relatively simple and not hold their own private data. &lt;br /&gt;
&lt;br /&gt;
In our example, this is certainly the case so we just set [[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] to &#039;&#039;&#039;YYYYMMDD00&#039;&#039;&#039; and forget about it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039;&amp;lt;br /&amp;gt; &lt;br /&gt;
Prior to version 1.5, the basic structure of each block class was slightly different. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== I Just Hear Static ==&lt;br /&gt;
In order to get our block to actually display something on screen, we need to add one more method to our class (before the final closing brace in our file). The new code is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
  function get_content() {&lt;br /&gt;
    if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
      return $this-&amp;gt;content;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;content         =  new stdClass;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
}   // Here&#039;s the closing curly bracket for the class definition&lt;br /&gt;
    // and here&#039;s the closing PHP tag from the section above.&lt;br /&gt;
?&amp;gt;  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can&#039;t get any simpler than that, can it? Let&#039;s dissect this method to see what&#039;s going on...&lt;br /&gt;
&lt;br /&gt;
First of all, there is a check that returns the current value of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] if it&#039;s not NULL; otherwise we proceed with &amp;quot;computing&amp;quot; it. Since the computation is potentially a time-consuming operation and it &#039;&#039;&#039;will&#039;&#039;&#039; be called several times for each block (Moodle works that way internally), we take a precaution and include this time-saver.&lt;br /&gt;
Supposing the content had not been computed before (it was NULL), we then define it from scratch. The code speaks for itself there, so there isn&#039;t much to say. Just keep in mind that we can use HTML both in the text &#039;&#039;&#039;and&#039;&#039;&#039; in the footer, if we want to.&lt;br /&gt;
&lt;br /&gt;
At this point our block should be capable of being automatically installed in Moodle and added to courses; visit your administration page to install it (Click &amp;quot;Notifications&amp;quot; under the Site Administration Block) and after seeing it in action come back to continue our tutorial.&lt;br /&gt;
&lt;br /&gt;
== Configure That Out ==&lt;br /&gt;
&lt;br /&gt;
The current version of our block doesn&#039;t really do much; it just displays a fixed message, which is not very useful. What we &#039;d really like to do is allow the teachers to customize what goes into the block. This, in block-speak, is called &amp;quot;instance configuration&amp;quot;. So let&#039;s give our block some instance configuration...&lt;br /&gt;
First of all, we need to tell Moodle that we want it to provide instance-specific configuration amenities to our block. That&#039;s as simple as adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This small change is enough to make Moodle display an &amp;quot;Edit...&amp;quot; icon in our block&#039;s header when we turn editing mode on in any course. However, if you try to click on that icon you will be presented with a notice that complains about the block&#039;s configuration not being implemented correctly. Try it, it&#039;s harmless.&lt;br /&gt;
Moodle&#039;s complaints do make sense. We told it that we want to have configuration, but we didn&#039;t say &#039;&#039;what&#039;&#039; kind of configuration we want, or how it should be displayed. To do that, we need to create one more file: &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/&#039;&#039;&#039;config_instance.html&#039;&#039;&#039;&amp;lt;/span&amp;gt; (which has to be named exactly like that). For the moment, copy paste the following into it and save:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;table cellpadding=&amp;quot;9&amp;quot; cellspacing=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_string(&#039;configcontent&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_textarea(true, 10, 50, 0, 0, &#039;text&#039;, $this-&amp;gt;config-&amp;gt;text); ?&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td colspan=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;) ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php use_html_editor(); ?&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It isn&#039;t difficult to see that the above code just provides us with a wysiwyg-editor-enabled textarea to write our block&#039;s desired content in and a submit button to save. But... what&#039;s $this-&amp;gt;config-&amp;gt;text? Well...&lt;br /&gt;
Moodle goes a long way to make things easier for block developers. Did you notice that the textarea is actually named &amp;quot;text&amp;quot;? When the submit button is pressed, Moodle saves each and every field it can find in our &#039;&#039;&#039;config_instance.html&#039;&#039;&#039; file as instance configuration data. &lt;br /&gt;
&lt;br /&gt;
We can then access that data as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;&#039;&#039;variablename&#039;&#039;&#039;&#039;&#039;, where &#039;&#039;variablename&#039;&#039; is the actual name we used for our field; in this case, &amp;quot;text&amp;quot;. So in essence, the above form just pre-populates the textarea with the current content of the block (as indeed it should) and then allows us to change it.&lt;br /&gt;
&lt;br /&gt;
You also might be surprised by the presence of a submit button and the absence of any &amp;lt;form&amp;gt; element at the same time. But the truth is, we don&#039;t need to worry about that at all; Moodle goes a really long way to make things easier for developers! We just print the configuration options we want, in any format we want; include a submit button, and Moodle will handle all the rest itself. The instance configuration variables are automatically at our disposal to access from any of the class methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29| init()]].&lt;br /&gt;
&lt;br /&gt;
In the event where the default behavior is not satisfactory, we can still override it. However, this requires advanced modifications to our block class and will not be covered here; refer to [[Development:Blocks/Appendix_A| Appendix A]] for more details.&lt;br /&gt;
Having now the ability to refer to this instance configuration data through [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]], the final twist is to tell our block to actually &#039;&#039;display&#039;&#039; what is saved in its configuration data. To do that, find this snippet in &#039;&#039;/blocks/simplehtml/block_simplehtml.php&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and change it to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Oh, and since the footer isn&#039;t really exciting at this point, we remove it from our block because it doesn&#039;t contribute anything. We could just as easily have decided to make the footer configurable in the above way, too. So for our latest code, the snippet becomes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After this discussion, our block is ready for prime time! Indeed, if you now visit any course with a SimpleHTML block, you will see that modifying its contents is now a snap.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Specialists ==&lt;br /&gt;
&lt;br /&gt;
Implementing instance configuration for the block&#039;s contents was good enough to whet our appetite, but who wants to stop there? Why not customize the block&#039;s title, too?&lt;br /&gt;
&lt;br /&gt;
Why not, indeed. Well, our first attempt to achieve this is natural enough: let&#039;s add another field to &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_instance.html&amp;lt;/span&amp;gt;. Here goes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&amp;lt;p&amp;gt;&lt;br /&gt;
    &amp;lt;?php print_string(&#039;configtitle&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;title&amp;quot; size=&amp;quot;30&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;config-&amp;gt;title; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We save the edited file, go to a course, edit the title of the block and... nothing happens! The instance configuration is saved correctly, all right (editing it once more proves that) but it&#039;s not being displayed. All we get is just the simple &amp;quot;SimpleHTML&amp;quot; title.&lt;br /&gt;
&lt;br /&gt;
That&#039;s not too weird, if we think back a bit. Do you remember that [[Development:Blocks/Appendix_A#init.28.29|init()]] method, where we set [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]]? We didn&#039;t actually change its value from then, and [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] is definitely not the same as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;title&#039;&#039;&#039; (to Moodle, at least). What we need is a way to update [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] with the value in the instance configuration. But as we said a bit earlier, we can use [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]] in all methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29|init()]]! So what can we do?&lt;br /&gt;
&lt;br /&gt;
Let&#039;s pull out another ace from our sleeve, and add this small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function specialization() {&lt;br /&gt;
  if(!empty($this-&amp;gt;config-&amp;gt;title)){&lt;br /&gt;
    $this-&amp;gt;title = $this-&amp;gt;config-&amp;gt;title;&lt;br /&gt;
  }else{&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;title = &#039;Some title ...&#039;;&lt;br /&gt;
  }&lt;br /&gt;
  if(empty($this-&amp;gt;config-&amp;gt;text)){&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;text = &#039;Some text ...&#039;;&lt;br /&gt;
  }    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Aha, here&#039;s what we wanted to do all along! But what&#039;s going on with the [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method?&lt;br /&gt;
&lt;br /&gt;
This &amp;quot;magic&amp;quot; method has actually a very nice property: it&#039;s &#039;&#039;guaranteed&#039;&#039; to be automatically called by Moodle as soon as our instance configuration is loaded and available (that is, immediately after [[Development:Blocks/Appendix_A#init.28.29|init()]] is called). That means before the block&#039;s content is computed for the first time, and indeed before &#039;&#039;anything&#039;&#039; else is done with the block. Thus, providing a [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method is the natural choice for any configuration data that needs to be acted upon &amp;quot;as soon as possible&amp;quot;, as in this case.&lt;br /&gt;
&lt;br /&gt;
== Now You See Me, Now You Don&#039;t ==&lt;br /&gt;
&lt;br /&gt;
Now would be a good time to mention another nifty technique that can be used in blocks, and which comes in handy quite often. Specifically, it may be the case that our block will have something interesting to display some of the time; but in some other cases, it won&#039;t have anything useful to say. (An example here would be the &amp;quot;Recent Activity&amp;quot; block, in the case where no recent activity in fact exists. &lt;br /&gt;
&lt;br /&gt;
However in that case the block chooses to explicitly inform you of the lack of said activity, which is arguably useful). It would be nice, then, to be able to have our block &amp;quot;disappear&amp;quot; if it&#039;s not needed to display it.&lt;br /&gt;
&lt;br /&gt;
This is indeed possible, and the way to do it is to make sure that after the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called, the block is completely void of content. Specifically, &amp;quot;void of content&amp;quot; means that both $this-&amp;gt;content-&amp;gt;text and $this-&amp;gt;content-&amp;gt;footer are each equal to the empty string (&amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt;). Moodle performs this check by calling the block&#039;s [[Development:Blocks/Appendix_A#is_empty.28.29| is_empty()]] method, and if the block is indeed empty then it is not displayed at all.&lt;br /&gt;
&lt;br /&gt;
Note that the exact value of the block&#039;s title and the presence or absence of a [[Development:Blocks/Appendix_A#hide_header.28.29| hide_header()]] method do &#039;&#039;not&#039;&#039; affect this behavior. A block is considered empty if it has no content, irrespective of anything else.&lt;br /&gt;
&lt;br /&gt;
== We Are Legion ==&lt;br /&gt;
&lt;br /&gt;
Right now our block is fully configurable, both in title and content. It&#039;s so versatile, in fact, that we could make pretty much anything out of it. It would be really nice to be able to add multiple blocks of this type to a single course. And, as you might have guessed, doing that is as simple as adding another small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_multiple() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Moodle that it should allow any number of instances of the SimpleHTML block in any course. After saving the changes to our file, Moodle immediately allows us to add multiple copies of the block without further ado!&lt;br /&gt;
&lt;br /&gt;
There are a couple more of interesting points to note here. First of all, even if a block itself allows multiple instances in the same page, the administrator still has the option of disallowing such behavior. This setting can be set separately for each block from the Administration / Configuration / Blocks page.&lt;br /&gt;
&lt;br /&gt;
And finally, a nice detail is that as soon as we defined an [[Development:Blocks/Appendix_A#instance_allow_multiple.28.29| instance_allow_multiple()]] method, the method [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] that was already defined became obsolete. &lt;br /&gt;
&lt;br /&gt;
Moodle assumes that if a block allows multiple instances of itself, those instances will want to be configured (what is the point of same multiple instances in the same page if they are identical?) and thus automatically provides an &amp;quot;Edit&amp;quot; icon. So, we can also remove the whole [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] method now without harm. We had only needed it when multiple instances of the block were not allowed.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Effects of Globalization ==&lt;br /&gt;
&lt;br /&gt;
Configuring each block instance with its own personal data is cool enough, but sometimes administrators need some way to &amp;quot;touch&amp;quot; all instances of a specific block at the same time. In the case of our SimpleHTML block, a few settings that would make sense to apply to all instances aren&#039;t that hard to come up with. &lt;br /&gt;
&lt;br /&gt;
For example, we might want to limit the contents of each block to only so many characters, or we might have a setting that filters HTML out of the block&#039;s contents, only allowing pure text in. Granted, such a feature wouldn&#039;t win us any awards for naming our block &amp;quot;SimpleHTML&amp;quot; but some tormented administrator somewhere might actually find it useful.&lt;br /&gt;
&lt;br /&gt;
This kind of configuration is called &amp;quot;global configuration&amp;quot; and applies only to a specific block type (all instances of that block type are affected, however). Implementing such configuration for our block is quite similar to implementing the instance configuration. We will now see how to implement the second example, having a setting that only allows text and not HTML in the block&#039;s contents.&lt;br /&gt;
First of all, we need to tell Moodle that we want our block to provide global configuration by, what a surprise, adding a small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function has_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we need to create a HTML file that actually prints out the configuration screen. In our case, we &#039;ll just print out a checkbox saying &amp;quot;Do not allow HTML in the content&amp;quot; and a &amp;quot;submit&amp;quot; button. Let&#039;s create the file &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_global.html&amp;lt;/span&amp;gt; which again must be named just so, and copy paste the following into it:&lt;br /&gt;
&lt;br /&gt;
[[Development_talk:Blocks|TODO: New settings.php method]] &lt;br /&gt;
: Just to note that general documentation about admin settings is at [[Development:Admin_settings#Individual_settings]]. In the absence of documentation, you can look at blocks/course_list, blocks/online_users and blocks/rss_client. They all use a settings.php file.--[[User:Tim Hunt|Tim Hunt]] 19:38, 28 January 2009 (CST)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;1&amp;quot;&lt;br /&gt;
   &amp;lt;?php if(!empty($CFG-&amp;gt;block_simplehtml_strict)) &lt;br /&gt;
             echo &#039;checked=&amp;quot;checked&amp;quot;&#039;; ?&amp;gt; /&amp;gt;&lt;br /&gt;
   &amp;lt;?php print_string(&#039;donotallowhtml&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;&lt;br /&gt;
 &amp;lt;p&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;); ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
True to our block&#039;s name, this looks simple enough. What it does is that it displays a checkbox named &amp;quot;block_simplehtml_strict&amp;quot; and if the Moodle configuration variable with the same name (i.e., $CFG-&amp;gt;block_simplehtml_strict) is set and not empty (that means it&#039;s not equal to an empty string, to zero, or to boolean FALSE) it displays the box as pre-checked (reflecting the current status). &lt;br /&gt;
&lt;br /&gt;
Why does it check the configuration setting with the same name? Because the default implementation of the global configuration saving code takes all the variables we have in our form and saves them as Moodle configuration options with the same name. Thus, it&#039;s good practice to use a descriptive name and also one that won&#039;t possibly conflict with the name of another setting. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;block_simplehtml_strict&amp;quot; clearly satisfies both requirements.&lt;br /&gt;
&lt;br /&gt;
The astute reader may have noticed that we actually have &#039;&#039;two&#039;&#039; input fields named &amp;quot;block_simplehtml_strict&amp;quot; in our configuration file. One is hidden and its value is always 0; the other is the checkbox and its value is 1. What gives? Why have them both there?&lt;br /&gt;
&lt;br /&gt;
Actually, this is a small trick we use to make our job as simple as possible. HTML forms work this way: if a checkbox in a form is not checked, its name does not appear at all in the variables passed to PHP when the form is submitted. That effectively means that, when we uncheck the box and click submit, the variable is not passed to PHP at all. Thus, PHP does not know to update its value to &amp;quot;0&amp;quot;, and our &amp;quot;strict&amp;quot; setting cannot be turned off at all once we turn it on for the first time. Not the behavior we want, surely.&lt;br /&gt;
&lt;br /&gt;
However, when PHP handles received variables from a form, the variables are processed in the order in which they appear in the form. If a variable comes up having the same name with an already-processed variable, the new value overwrites the old one. Taking advantage of this, our logic runs as follows: the variable &amp;quot;block_simplehtml_strict&amp;quot; is first unconditionally set to &amp;quot;0&amp;quot;. Then, &#039;&#039;if&#039;&#039; the box is checked, it is set to &amp;quot;1&amp;quot;, overwriting the previous value as discussed. The net result is that our configuration setting behaves as it should.&lt;br /&gt;
&lt;br /&gt;
To round our bag of tricks up, notice that the use of &#039;&#039;if(!empty($CFG-&amp;gt;block_simplehtml_strict))&#039;&#039; in the test for &amp;quot;should the box be checked by default?&amp;quot; is quite deliberate. The first time this script runs, the variable &#039;&#039;&#039;$CFG-&amp;gt;block_simplehtml_strict&#039;&#039;&#039; will not exist at all. After it&#039;s set for the first time, its value can be either &amp;quot;0&amp;quot; or &amp;quot;1&amp;quot;. Given that both &amp;quot;not set&amp;quot; and the string &amp;quot;0&amp;quot; evaluate as empty while the sting &amp;quot;1&amp;quot; does not, we manage to avoid any warnings from PHP regarding the variable not being set at all, &#039;&#039;and&#039;&#039; have a nice human-readable representation for its two possible values (&amp;quot;0&amp;quot; and &amp;quot;1&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
=== config_save() ===&lt;br /&gt;
&lt;br /&gt;
Now that we have managed to cram a respectable amount of tricks into a few lines of HTML, we might as well discuss the alternative in case that tricks are not enough for a specific configuration setup we have in mind. Saving the data is done in the method [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], the default implementation of which is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  // Default behavior: save all variables as $CFG properties&lt;br /&gt;
  foreach ($data as $name =&amp;gt; $value) {&lt;br /&gt;
    set_config($name, $value);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As can be clearly seen, Moodle passes this method an associative array $data which contains all the variables coming in from our configuration screen. If we wanted to do the job without the &amp;quot;hidden variable with the same name&amp;quot; trick we used above, one way to do it would be by overriding this method with the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  if(isset($data[&#039;block_simplehtml_strict&#039;])) {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;1&#039;);&lt;br /&gt;
  }else {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;0&#039;);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quite straightfoward: if the variable &amp;quot;block_simplehtml_strict&amp;quot; is passed to us, then it can only mean that the user has checked it, so set the configuration variable with the same name to &amp;quot;1&amp;quot;. Otherwise, set it to &amp;quot;0&amp;quot;. Of course, this version would need to be updated if we add more configuration options because it doesn&#039;t respond to them as the default implementation does. Still, it&#039;s useful to know how we can override the default implementation if it does not fit our needs (for example, we might not want to save the variable as part of the Moodle configuration but do something else with it).&lt;br /&gt;
&lt;br /&gt;
So, we are now at the point where we know if the block should allow HTML tags in its content or not. How do we get the block to actually respect that setting?&lt;br /&gt;
&lt;br /&gt;
We could decide to do one of two things: either have the block &amp;quot;clean&amp;quot; HTML out from the input before saving it in the instance configuration and then display it as-is (the &amp;quot;eager&amp;quot; approach); or have it save the data &amp;quot;as is&amp;quot; and then clean it up each time just before displaying it (the &amp;quot;lazy&amp;quot; approach). The eager approach involves doing work once when saving the configuration; the lazy approach means doing work each time the block is displayed and thus it promises to be worse performance-wise. We shall hence go with the eager approach.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
=== instance_config_save() ===&lt;br /&gt;
&lt;br /&gt;
Much as we did just before with overriding [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], what is needed here is overriding the method [[Development:Blocks/Appendix_A#instance_config_save.28.29| instance_config_save()]] which handles the instance configuration. The default implementation is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  $data = stripslashes_recursive($data);&lt;br /&gt;
  $this-&amp;gt;config = $data;&lt;br /&gt;
  return set_field(&#039;block_instance&#039;, &lt;br /&gt;
                   &#039;configdata&#039;,&lt;br /&gt;
                    base64_encode(serialize($data)),&lt;br /&gt;
                   &#039;id&#039;, &lt;br /&gt;
                   $this-&amp;gt;instance-&amp;gt;id);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This may look intimidating at first (what&#039;s all this stripslashes_recursive() and base64_encode() and serialize() stuff?) but do not despair; we won&#039;t have to touch any of it. We will only add some extra validation code in the beginning and then instruct Moodle to additionally call this default implementation to do the actual storing of the data. Specifically, we will add a method to our class which goes like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  // Clean the data if we have to&lt;br /&gt;
  global $CFG;&lt;br /&gt;
  if(!empty($CFG-&amp;gt;block_simplehtml_strict)) {&lt;br /&gt;
    $data-&amp;gt;text = strip_tags($data-&amp;gt;text);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // And now forward to the default implementation defined in the parent class&lt;br /&gt;
  return parent::instance_config_save($data);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At last! Now the administrator has absolute power of life and death over what type of content is allowed in our &amp;quot;SimpleHTML&amp;quot; block! Absolute? Well... not exactly. In fact, if we think about it for a while, it will become apparent that if at some point in time HTML is allowed and some blocks have saved their content with HTML included, and afterwards the administrator changes the setting to &amp;quot;off&amp;quot;, this will only prevent subsequent content changes from including HTML. Blocks which already had HTML in their content would continue to display it!&lt;br /&gt;
&lt;br /&gt;
Following that train of thought, the next stop is realizing that we wouldn&#039;t have this problem if we had chosen the lazy approach a while back, because in that case we would &amp;quot;sanitize&amp;quot; each block&#039;s content just before it was displayed. &lt;br /&gt;
&lt;br /&gt;
The only thing we can do with the eager approach is strip all the tags from the content of all SimpleHTML instances as soon as the admin setting is changed to &amp;quot;HTML off&amp;quot;; but even then, turning the setting back to &amp;quot;HTML on&amp;quot; won&#039;t bring back the tags we stripped away. On the other hand, the lazy approach might be slower, but it&#039;s more versatile; we can choose whether to strip or keep the HTML before displaying the content, and we won&#039;t lose it at all if the admin toggles the setting off and on again. Isn&#039;t the life of a developer simple and wonderful?&lt;br /&gt;
&lt;br /&gt;
=== Exercise === &lt;br /&gt;
We will let this part of the tutorial come to a close with the obligatory exercise for the reader: &lt;br /&gt;
In order to have the SimpleHTML block work &amp;quot;correctly&amp;quot;, find out how to strengthen the eager approach to strip out all tags from the existing configuration of all instances of our block, &#039;&#039;&#039;or&#039;&#039;&#039; go back and implement the lazy approach instead. &lt;br /&gt;
(Hint: Do that in the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method.)&lt;br /&gt;
&lt;br /&gt;
=== UPDATING: === &lt;br /&gt;
Prior to version 1.5, the file &#039;&#039;config_global.html&#039;&#039; was named simply &#039;&#039;config.html&#039;&#039;. Also, the methods [[Blocks_Howto#method_config_save| config_save]] and [[Blocks_Howto#method_config_print| config_print]] were named &#039;&#039;&#039;handle_config&#039;&#039;&#039; and &#039;&#039;&#039;print_config&#039;&#039;&#039; respectively. Upgrading a block to work with Moodle 1.5 involves updating these aspects; refer to [[Blocks_Howto#appendix_b| Appendix B]] for more information.&lt;br /&gt;
&lt;br /&gt;
== Eye Candy ==&lt;br /&gt;
&lt;br /&gt;
Our block is just about complete functionally, so now let&#039;s take a look at some of the tricks we can use to make its behavior customized in a few more useful ways.&lt;br /&gt;
&lt;br /&gt;
First of all, there are a couple of ways we can adjust the visual aspects of our block. For starters, it might be useful to create a block that doesn&#039;t display a header (title) at all. You can see this effect in action in the Course Description block that comes with Moodle. This behavior is achieved by, you guessed it, adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function hide_header() {&lt;br /&gt;
  return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One more note here: we cannot just set an empty title inside the block&#039;s [[Development:Blocks/Appendix_A#init.28.29| init()]] method; it&#039;s necessary for each block to have a unique, non-empty title after [[Development:Blocks/Appendix_A#init.28.29| init()]] is called so that Moodle can use those titles to differentiate between all of the installed blocks.&lt;br /&gt;
&lt;br /&gt;
Another adjustment we might want to do is instruct our block to take up a certain amount of width on screen. Moodle handles this as a two-part process: first, it queries each block about its preferred width and takes the maximum number as the desired value. Then, the page that&#039;s being displayed can choose to use this value or, more probably, bring it within some specific range of values if it isn&#039;t already. That means that the width setting is a best-effort settlement; your block can &#039;&#039;request&#039;&#039; a certain width and Moodle will &#039;&#039;try&#039;&#039; to provide it, but there&#039;s no guarantee whatsoever about the end result. As a concrete example, all standard Moodle course formats will deliver any requested width between 180 and 210 pixels, inclusive.&lt;br /&gt;
&lt;br /&gt;
To instruct Moodle about our block&#039;s preferred width, we add one more method to the block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function preferred_width() {&lt;br /&gt;
  // The preferred value is in pixels&lt;br /&gt;
  return 200;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This will make our block (and all the other blocks displayed at the same side of the page) a bit wider than standard.&lt;br /&gt;
&lt;br /&gt;
Finally, we can also affect some properties of the actual HTML that will be used to print our block. Each block is fully contained within a &amp;amp;lt;table&amp;amp;gt; element, inside which all the HTML for that block is printed. We can instruct Moodle to add HTML attributes with specific values to that container. This would be done to either a) directly affect the end result (if we say, assign bgcolor=&amp;quot;black&amp;quot;), or b) give us freedom to customize the end result using CSS (this is in fact done by default as we &#039;ll see below).&lt;br /&gt;
&lt;br /&gt;
The default behavior of this feature in our case will assign to our block&#039;s container the class HTML attribute with the value &amp;quot;sideblock block_simplehtml&amp;quot; (the prefix &amp;quot;block_&amp;quot; followed by the name of our block, lowercased). We can then use that class to make CSS selectors in our theme to alter this block&#039;s visual style (for example, &amp;quot;.sideblock.block_simplehtml { border: 1px black solid}&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To change the default behavior, we will need to define a method which returns an associative array of attribute names and values. For example, the version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
  return array(&lt;br /&gt;
    &#039;class&#039;       =&amp;gt; &#039;sideblock block_&#039;. $this-&amp;gt;name(),&lt;br /&gt;
    &#039;onmouseover&#039; =&amp;gt; &amp;quot;alert(&#039;Mouseover on our block!&#039;);&amp;quot;&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will result in a mouseover event being added to our block using JavaScript, just as if we had written the onmouseover=&amp;quot;alert(...)&amp;quot; part ourselves in HTML. Note that we actually duplicate the part which sets the class attribute (we want to keep that, and since we override the default behavior it&#039;s our responsibility to emulate it if required). &lt;br /&gt;
&lt;br /&gt;
And the final elegant touch is that we don&#039;t set the class to the hard-coded value &amp;quot;block_simplehtml&amp;quot; but instead use the [[Development:Blocks/Appendix_A#name.28.29| name()]] method to make it dynamically match our block&#039;s name.&lt;br /&gt;
&lt;br /&gt;
== Authorized Personnel Only ==&lt;br /&gt;
&lt;br /&gt;
It&#039;s not difficult to imagine a block which is very useful in some circumstances but it simply cannot be made meaningful in others. An example of this would be the &amp;quot;Social Activities&amp;quot; block which is indeed useful in a course with the social format, but doesn&#039;t do anything useful in a course with the weeks format. There should be some way of allowing the use of such blocks only where they are indeed meaningful, and not letting them confuse users if they are not.&lt;br /&gt;
&lt;br /&gt;
Moodle allows us to declare which course formats each block is allowed to be displayed in, and enforces these restrictions as set by the block developers at all times. The information is given to Moodle as a standard associative array, with each key corresponding to a page format and defining a boolean value (true/false) that declares whether the block should be allowed to appear in that page format.&lt;br /&gt;
&lt;br /&gt;
Notice the deliberate use of the term &#039;&#039;page&#039;&#039; instead of &#039;&#039;course&#039;&#039; in the above paragraph. This is because in Moodle 1.5 and onwards, blocks can be displayed in any page that supports them. The best example of such pages are the course pages, but we are not restricted to them. For instance, the quiz view page (the first one we see when we click on the name of the quiz) also supports blocks.&lt;br /&gt;
&lt;br /&gt;
The format names we can use for the pages derive from the name of the script which is actually used to display that page. For example, when we are looking at a course, the script is &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/course/view.php&amp;lt;/span&amp;gt; (this is evident from the browser&#039;s address line). Thus, the format name of that page is &#039;&#039;&#039;course-view&#039;&#039;&#039;. It follows easily that the format name for a quiz view page is &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039;. This rule of thumb does have a few exceptions, however:&lt;br /&gt;
&lt;br /&gt;
# The format name for the front page of Moodle is &#039;&#039;&#039;site-index&#039;&#039;&#039;.&lt;br /&gt;
# The format name for courses is actually not just &#039;&#039;&#039;course-view&#039;&#039;&#039;&amp;lt;nowiki&amp;gt;; it is &amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;course-view-weeks&#039;&#039;&#039;, &#039;&#039;&#039;course-view-topics&#039;&#039;&#039;, etc.&lt;br /&gt;
# Even though there is no such page, the format name &#039;&#039;&#039;all&#039;&#039;&#039; can be used as a catch-all option.&lt;br /&gt;
&lt;br /&gt;
We can include as many format names as we want in our definition of the applicable formats. Each format can be allowed or disallowed, and there are also three more rules that help resolve the question &amp;quot;is this block allowed into this page or not?&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
# Prefixes of a format name will match that format name; for example, &#039;&#039;&#039;mod&#039;&#039;&#039; will match all the activity modules. &#039;&#039;&#039;course-view&#039;&#039;&#039; will match any course, regardless of the course format. And finally, &#039;&#039;&#039;site&#039;&#039;&#039; will also match the front page (remember that its full format name is &#039;&#039;&#039;site-index&#039;&#039;&#039;).&lt;br /&gt;
# The more specialized a format name that matches our page is, the higher precedence it has when deciding if the block will be allowed. For example, &#039;&#039;&#039;mod&#039;&#039;&#039;, &#039;&#039;&#039;mod-quiz&#039;&#039;&#039; and &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; all match the quiz view page. But if all three are present, &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; will take precedence over the other two because it is a better match.&lt;br /&gt;
# The character &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; can be used in place of any word. For example, &#039;&#039;&#039;mod&#039;&#039;&#039; and &#039;&#039;&#039;mod-*&#039;&#039;&#039; are equivalent. At the time of this document&#039;s writing, there is no actual reason to utilize this &amp;quot;wildcard matching&amp;quot; feature, but it exists for future usage.&lt;br /&gt;
# The order that the format names appear does not make any difference.&lt;br /&gt;
All of the above are enough to make the situation sound complex, so let&#039;s look at some specific examples. First of all, to have our block appear &#039;&#039;&#039;only&#039;&#039;&#039; in the site front page, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&#039;site&#039; =&amp;gt; TRUE);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Since &#039;&#039;&#039;all&#039;&#039;&#039; is missing, the block is disallowed from appearing in &#039;&#039;any&#039;&#039; course format; but then &#039;&#039;&#039;site&#039;&#039;&#039; is set to TRUE, so it&#039;s explicitly allowed to appear in the site front page (remember that &#039;&#039;&#039;site&#039;&#039;&#039; matches &#039;&#039;&#039;site-index&#039;&#039;&#039; because it&#039;s a prefix).&lt;br /&gt;
&lt;br /&gt;
For another example, if we wanted to allow the block to appear in all course formats &#039;&#039;except&#039;&#039; social, and also to &#039;&#039;not&#039;&#039; be allowed anywhere but in courses, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;course-view&#039; =&amp;gt; TRUE, &lt;br /&gt;
    &#039;course-view-social&#039; =&amp;gt; FALSE);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This time, we first allow the block to appear in all courses and then we explicitly disallow the social format.&lt;br /&gt;
For our final, most complicated example, suppose that a block can be displayed in the site front page, in courses (but not social courses) and also when we are viewing any activity module, &#039;&#039;except&#039;&#039; quiz. This would be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;site-index&#039; =&amp;gt; TRUE,&lt;br /&gt;
          &#039;course-view&#039; =&amp;gt; TRUE, &lt;br /&gt;
   &#039;course-view-social&#039; =&amp;gt; FALSE,&lt;br /&gt;
                  &#039;mod&#039; =&amp;gt; TRUE, &lt;br /&gt;
             &#039;mod-quiz&#039; =&amp;gt; FALSE&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is not difficult to realize that the above accomplishes the objective if we remember that there is a &amp;quot;best match&amp;quot; policy to determine the end result.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039; &amp;lt;br /&amp;gt;&lt;br /&gt;
Prior to version 1.5, blocks were only allowed in courses (and in Moodle 1.4, in the site front page). Also, the keywords used to describe the valid course formats at the time were slightly different and had to be changed in order to allow for a more open architecture. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== Lists and Icons ==&lt;br /&gt;
&lt;br /&gt;
In this final part of the guide we will briefly discuss an additional capability of Moodle&#039;s block system, namely the ability to very easily create blocks that display a list of choices to the user. This list is displayed with one item per line, and an optional image (icon) next to the item. An example of such a &#039;&#039;list block&#039;&#039; is the standard Moodle &amp;quot;admin&amp;quot; block, which illustrates all the points discussed in this section.&lt;br /&gt;
&lt;br /&gt;
As we have seen so far, blocks use two properties of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]]: &amp;quot;text&amp;quot; and &amp;quot;footer&amp;quot;. The text is displayed as-is as the block content, and the footer is displayed below the content in a smaller font size. List blocks use $this-&amp;gt;content-&amp;gt;footer in the exact same way, but they ignore $this-&amp;gt;content-&amp;gt;text.&lt;br /&gt;
&lt;br /&gt;
Instead, Moodle expects such blocks to set two other properties when the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called: $this-&amp;gt;content-&amp;gt;items and $this-&amp;gt;content-&amp;gt;icons. $this-&amp;gt;content-&amp;gt;items should be a numerically indexed array containing elements that represent the HTML for each item in the list that is going to be displayed. Usually these items will be HTML anchor tags which provide links to some page. $this-&amp;gt;content-&amp;gt;icons should also be a numerically indexed array, with exactly as many items as $this-&amp;gt;content-&amp;gt;items has. Each of these items should be a fully qualified HTML &amp;lt;img&amp;gt; tag, with &amp;quot;src&amp;quot;, &amp;quot;height&amp;quot;, &amp;quot;width&amp;quot; and &amp;quot;alt&amp;quot; attributes. Obviously, it makes sense to keep the images small and of a uniform size.&lt;br /&gt;
&lt;br /&gt;
In order to tell Moodle that we want to have a list block instead of the standard text block, we need to make a small change to our block class declaration. Instead of extending class &#039;&#039;&#039;block_base&#039;&#039;&#039;, our block will extend class &#039;&#039;&#039;block_list&#039;&#039;&#039;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
 class block_my_menu extends block_list {&lt;br /&gt;
     // The init() method does not need to change at all&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to making this change, we must of course also modify the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method to construct the [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] variable as discussed above:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function get_content() {&lt;br /&gt;
  if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content         = new stdClass;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items[] = &#039;&amp;lt;a href=&amp;quot;some_file.php&amp;quot;&amp;gt;Menu Option 1&amp;lt;/a&amp;gt;&#039;;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons[] = &#039;&amp;lt;img src=&amp;quot;images/icons/1.gif&amp;quot; class=&amp;quot;icon&amp;quot; alt=&amp;quot;&amp;quot; /&amp;gt;&#039;;&lt;br /&gt;
 &lt;br /&gt;
  // Add more list items here&lt;br /&gt;
 &lt;br /&gt;
  return $this-&amp;gt;content;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To summarize, if we want to create a list block instead of a text block, we just need to change the block class declaration and the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method. Adding the mandatory [[Development:Blocks/Appendix_A#init.28.29| init()]] method as discussed earlier will then give us our first list block in no time!&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== Appendices ==&lt;br /&gt;
&lt;br /&gt;
The appendices have been moved to separate pages:&lt;br /&gt;
&lt;br /&gt;
* Appendix A: [[Development:Blocks/Appendix A|&#039;&#039;block_base&#039;&#039; Reference]] &lt;br /&gt;
* Appendix B: [[Development:Blocks/Appendix B|Differences in the Blocks API for Moodle Versions prior to 1.5]]&lt;br /&gt;
* Appendix C: [[Development:Blocks/Appendix C|Creating Database Tables for Blocks (prior to 1.7)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Blocks]]&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
&lt;br /&gt;
[[es:Desarrollo de bloques]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55112</id>
		<title>Broken/Blocks</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55112"/>
		<updated>2009-04-30T10:36:54Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* We Are Legion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; A Step-by-step Guide To Creating Blocks &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Original Author: Jon Papaioannou (pj@moodle.org)&lt;br /&gt;
&lt;br /&gt;
The present document serves as a guide to developers who want to create their own blocks for use in Moodle. It applies to the 1.5 development version of Moodle (and any newer) &#039;&#039;&#039;only&#039;&#039;&#039;, as the blocks subsystem was rewritten and expanded for the 1.5 release. However, you can also find it useful if you want to modify blocks written for Moodle 1.3 and 1.4 to work with the latest versions (look at [[Development:Blocks/Appendix_B| Appendix B]]).&lt;br /&gt;
&lt;br /&gt;
The guide is written as an interactive course which aims to develop a configurable, multi-purpose block that displays arbitrary HTML. It&#039;s targeted mainly at people with little experience with Moodle or programming in general and aims to show how easy it is to create new blocks for Moodle. A certain small amount of PHP programming knowledge is still required, though. &lt;br /&gt;
&lt;br /&gt;
Experienced developers and those who just want a reference text should refer to [[Development:Blocks/Appendix_A| Appendix A]] because the main guide has a rather low concentration of pure information in the text.&lt;br /&gt;
&lt;br /&gt;
== Basic Concepts ==&lt;br /&gt;
&lt;br /&gt;
Through this guide, we will be following the creation of an &amp;quot;HTML&amp;quot; block from scratch in order to demonstrate most of the block features at our disposal. Our block will be named &amp;quot;SimpleHTML&amp;quot;. This does not constrain us regarding the name of the actual directory on the server where the files for our block will be stored, but for consistency we will follow the practice of using the lowercased form &amp;quot;simplehtml&amp;quot; in any case where such a name is required. &lt;br /&gt;
&lt;br /&gt;
Whenever we refer to a file or directory name which contains &amp;quot;simplehtml&amp;quot;, it&#039;s important to remember that &#039;&#039;only&#039;&#039; the &amp;quot;simplehtml&amp;quot; part is up to us to change; the rest is standardized and essential for Moodle to work correctly.&lt;br /&gt;
&lt;br /&gt;
Whenever a file&#039;s path is mentioned in this guide, it will always start with a slash. This refers to the Moodle home directory; all files and directories will be referred to with respect to that directory.&lt;br /&gt;
&lt;br /&gt;
== Ready, Set, Go! ==&lt;br /&gt;
&lt;br /&gt;
To define a &amp;quot;block&amp;quot; in Moodle, in the most basic case we need to provide just one source code file. We start by creating the directory &#039;&#039;/blocks/simplehtml/&#039;&#039; and creating a file named &#039;&#039;/blocks/simplehtml/&#039;&#039;&#039;&#039;&#039;block_simplehtml.php&#039;&#039;&#039; which will hold our code. We then begin coding the block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class block_simplehtml extends block_base {&lt;br /&gt;
  function init() {&lt;br /&gt;
    $this-&amp;gt;title   = get_string(&#039;simplehtml&#039;, &#039;block_simplehtml&#039;);&lt;br /&gt;
    $this-&amp;gt;version = 2004111200;&lt;br /&gt;
  }&lt;br /&gt;
  // The PHP tag and the curly bracket for the class definition &lt;br /&gt;
  // will only be closed after there is another function added in the next section.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first line is our block class definition; it must be named exactly in the manner shown. Again, only the &amp;quot;simplehtml&amp;quot; part can (and indeed must) change; everything else is standardized.&lt;br /&gt;
&lt;br /&gt;
Our class is then given a small method: [[Development:Blocks/Appendix_A#init.28.29| init()]]. This is essential for all blocks, and its purpose is to set the two class member variables listed inside it. But what do these values actually mean? Here&#039;s a more detailed description.&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Etitle| $this-&amp;gt;title]] is the title displayed in the header of our block. We can set it to whatever we like; in this case it&#039;s set to read the actual title from a language file we are presumably distributing together with the block. I &#039;ll skip ahead a bit here and say that if you want your block to display &#039;&#039;&#039;no&#039;&#039;&#039; title at all, then you should set this to any descriptive value you want (but &#039;&#039;&#039;not&#039;&#039;&#039; make it an empty string). We will later see [[Development:Blocks#Eye_Candy| how to disable the title&#039;s display]].&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] is the version of our block. This actually would only make a difference if your block wanted to keep its own data in special tables in the database (i.e. for very complex blocks). In that case the version number is used exactly as it&#039;s used in activities; an upgrade script uses it to incrementally upgrade an &amp;quot;old&amp;quot; version of the block&#039;s data to the latest. We will outline this process further ahead, since blocks tend to be relatively simple and not hold their own private data. &lt;br /&gt;
&lt;br /&gt;
In our example, this is certainly the case so we just set [[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] to &#039;&#039;&#039;YYYYMMDD00&#039;&#039;&#039; and forget about it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039;&amp;lt;br /&amp;gt; &lt;br /&gt;
Prior to version 1.5, the basic structure of each block class was slightly different. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== I Just Hear Static ==&lt;br /&gt;
In order to get our block to actually display something on screen, we need to add one more method to our class (before the final closing brace in our file). The new code is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
  function get_content() {&lt;br /&gt;
    if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
      return $this-&amp;gt;content;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;content         =  new stdClass;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
}   // Here&#039;s the closing curly bracket for the class definition&lt;br /&gt;
    // and here&#039;s the closing PHP tag from the section above.&lt;br /&gt;
?&amp;gt;  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can&#039;t get any simpler than that, can it? Let&#039;s dissect this method to see what&#039;s going on...&lt;br /&gt;
&lt;br /&gt;
First of all, there is a check that returns the current value of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] if it&#039;s not NULL; otherwise we proceed with &amp;quot;computing&amp;quot; it. Since the computation is potentially a time-consuming operation and it &#039;&#039;&#039;will&#039;&#039;&#039; be called several times for each block (Moodle works that way internally), we take a precaution and include this time-saver.&lt;br /&gt;
Supposing the content had not been computed before (it was NULL), we then define it from scratch. The code speaks for itself there, so there isn&#039;t much to say. Just keep in mind that we can use HTML both in the text &#039;&#039;&#039;and&#039;&#039;&#039; in the footer, if we want to.&lt;br /&gt;
&lt;br /&gt;
At this point our block should be capable of being automatically installed in Moodle and added to courses; visit your administration page to install it (Click &amp;quot;Notifications&amp;quot; under the Site Administration Block) and after seeing it in action come back to continue our tutorial.&lt;br /&gt;
&lt;br /&gt;
== Configure That Out ==&lt;br /&gt;
&lt;br /&gt;
The current version of our block doesn&#039;t really do much; it just displays a fixed message, which is not very useful. What we &#039;d really like to do is allow the teachers to customize what goes into the block. This, in block-speak, is called &amp;quot;instance configuration&amp;quot;. So let&#039;s give our block some instance configuration...&lt;br /&gt;
First of all, we need to tell Moodle that we want it to provide instance-specific configuration amenities to our block. That&#039;s as simple as adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This small change is enough to make Moodle display an &amp;quot;Edit...&amp;quot; icon in our block&#039;s header when we turn editing mode on in any course. However, if you try to click on that icon you will be presented with a notice that complains about the block&#039;s configuration not being implemented correctly. Try it, it&#039;s harmless.&lt;br /&gt;
Moodle&#039;s complaints do make sense. We told it that we want to have configuration, but we didn&#039;t say &#039;&#039;what&#039;&#039; kind of configuration we want, or how it should be displayed. To do that, we need to create one more file: &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/&#039;&#039;&#039;config_instance.html&#039;&#039;&#039;&amp;lt;/span&amp;gt; (which has to be named exactly like that). For the moment, copy paste the following into it and save:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;table cellpadding=&amp;quot;9&amp;quot; cellspacing=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_string(&#039;configcontent&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_textarea(true, 10, 50, 0, 0, &#039;text&#039;, $this-&amp;gt;config-&amp;gt;text); ?&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td colspan=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;) ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php use_html_editor(); ?&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It isn&#039;t difficult to see that the above code just provides us with a wysiwyg-editor-enabled textarea to write our block&#039;s desired content in and a submit button to save. But... what&#039;s $this-&amp;gt;config-&amp;gt;text? Well...&lt;br /&gt;
Moodle goes a long way to make things easier for block developers. Did you notice that the textarea is actually named &amp;quot;text&amp;quot;? When the submit button is pressed, Moodle saves each and every field it can find in our &#039;&#039;&#039;config_instance.html&#039;&#039;&#039; file as instance configuration data. &lt;br /&gt;
&lt;br /&gt;
We can then access that data as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;&#039;&#039;variablename&#039;&#039;&#039;&#039;&#039;, where &#039;&#039;variablename&#039;&#039; is the actual name we used for our field; in this case, &amp;quot;text&amp;quot;. So in essence, the above form just pre-populates the textarea with the current content of the block (as indeed it should) and then allows us to change it.&lt;br /&gt;
&lt;br /&gt;
You also might be surprised by the presence of a submit button and the absence of any &amp;lt;form&amp;gt; element at the same time. But the truth is, we don&#039;t need to worry about that at all; Moodle goes a really long way to make things easier for developers! We just print the configuration options we want, in any format we want; include a submit button, and Moodle will handle all the rest itself. The instance configuration variables are automatically at our disposal to access from any of the class methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29| init()]].&lt;br /&gt;
&lt;br /&gt;
In the event where the default behavior is not satisfactory, we can still override it. However, this requires advanced modifications to our block class and will not be covered here; refer to [[Development:Blocks/Appendix_A| Appendix A]] for more details.&lt;br /&gt;
Having now the ability to refer to this instance configuration data through [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]], the final twist is to tell our block to actually &#039;&#039;display&#039;&#039; what is saved in its configuration data. To do that, find this snippet in &#039;&#039;/blocks/simplehtml/block_simplehtml.php&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and change it to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Oh, and since the footer isn&#039;t really exciting at this point, we remove it from our block because it doesn&#039;t contribute anything. We could just as easily have decided to make the footer configurable in the above way, too. So for our latest code, the snippet becomes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After this discussion, our block is ready for prime time! Indeed, if you now visit any course with a SimpleHTML block, you will see that modifying its contents is now a snap.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Specialists ==&lt;br /&gt;
&lt;br /&gt;
Implementing instance configuration for the block&#039;s contents was good enough to whet our appetite, but who wants to stop there? Why not customize the block&#039;s title, too?&lt;br /&gt;
&lt;br /&gt;
Why not, indeed. Well, our first attempt to achieve this is natural enough: let&#039;s add another field to &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_instance.html&amp;lt;/span&amp;gt;. Here goes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&amp;lt;p&amp;gt;&lt;br /&gt;
    &amp;lt;?php print_string(&#039;configtitle&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;title&amp;quot; size=&amp;quot;30&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;config-&amp;gt;title; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We save the edited file, go to a course, edit the title of the block and... nothing happens! The instance configuration is saved correctly, all right (editing it once more proves that) but it&#039;s not being displayed. All we get is just the simple &amp;quot;SimpleHTML&amp;quot; title.&lt;br /&gt;
&lt;br /&gt;
That&#039;s not too weird, if we think back a bit. Do you remember that [[Development:Blocks/Appendix_A#init.28.29|init()]] method, where we set [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]]? We didn&#039;t actually change its value from then, and [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] is definitely not the same as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;title&#039;&#039;&#039; (to Moodle, at least). What we need is a way to update [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] with the value in the instance configuration. But as we said a bit earlier, we can use [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]] in all methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29|init()]]! So what can we do?&lt;br /&gt;
&lt;br /&gt;
Let&#039;s pull out another ace from our sleeve, and add this small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function specialization() {&lt;br /&gt;
  if(!empty($this-&amp;gt;config-&amp;gt;title)){&lt;br /&gt;
    $this-&amp;gt;title = $this-&amp;gt;config-&amp;gt;title;&lt;br /&gt;
  }else{&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;title = &#039;Some title ...&#039;;&lt;br /&gt;
  }&lt;br /&gt;
  if(empty($this-&amp;gt;config-&amp;gt;text)){&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;text = &#039;Some text ...&#039;;&lt;br /&gt;
  }    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Aha, here&#039;s what we wanted to do all along! But what&#039;s going on with the [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method?&lt;br /&gt;
&lt;br /&gt;
This &amp;quot;magic&amp;quot; method has actually a very nice property: it&#039;s &#039;&#039;guaranteed&#039;&#039; to be automatically called by Moodle as soon as our instance configuration is loaded and available (that is, immediately after [[Development:Blocks/Appendix_A#init.28.29|init()]] is called). That means before the block&#039;s content is computed for the first time, and indeed before &#039;&#039;anything&#039;&#039; else is done with the block. Thus, providing a [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method is the natural choice for any configuration data that needs to be acted upon &amp;quot;as soon as possible&amp;quot;, as in this case.&lt;br /&gt;
&lt;br /&gt;
== Now You See Me, Now You Don&#039;t ==&lt;br /&gt;
&lt;br /&gt;
Now would be a good time to mention another nifty technique that can be used in blocks, and which comes in handy quite often. Specifically, it may be the case that our block will have something interesting to display some of the time; but in some other cases, it won&#039;t have anything useful to say. (An example here would be the &amp;quot;Recent Activity&amp;quot; block, in the case where no recent activity in fact exists. &lt;br /&gt;
&lt;br /&gt;
However in that case the block chooses to explicitly inform you of the lack of said activity, which is arguably useful). It would be nice, then, to be able to have our block &amp;quot;disappear&amp;quot; if it&#039;s not needed to display it.&lt;br /&gt;
&lt;br /&gt;
This is indeed possible, and the way to do it is to make sure that after the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called, the block is completely void of content. Specifically, &amp;quot;void of content&amp;quot; means that both $this-&amp;gt;content-&amp;gt;text and $this-&amp;gt;content-&amp;gt;footer are each equal to the empty string (&amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt;). Moodle performs this check by calling the block&#039;s [[Development:Blocks/Appendix_A#is_empty.28.29| is_empty()]] method, and if the block is indeed empty then it is not displayed at all.&lt;br /&gt;
&lt;br /&gt;
Note that the exact value of the block&#039;s title and the presence or absence of a [[Development:Blocks/Appendix_A#hide_header.28.29| hide_header()]] method do &#039;&#039;not&#039;&#039; affect this behavior. A block is considered empty if it has no content, irrespective of anything else.&lt;br /&gt;
&lt;br /&gt;
== We Are Legion ==&lt;br /&gt;
&lt;br /&gt;
Right now our block is fully configurable, both in title and content. It&#039;s so versatile, in fact, that we could make pretty much anything out of it. It would be really nice to be able to add multiple blocks of this type to a single course. And, as you might have guessed, doing that is as simple as adding another small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_multiple() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Moodle that it should allow any number of instances of the SimpleHTML block in any course. After saving the changes to our file, Moodle immediately allows us to add multiple copies of the block without further ado!&lt;br /&gt;
&lt;br /&gt;
There are a couple more of interesting points to note here. First of all, even if a block itself allows multiple instances in the same page, the administrator still has the option of disallowing such behavior. This setting can be set separately for each block from the Administration / Configuration / Blocks page.&lt;br /&gt;
&lt;br /&gt;
And finally, a nice detail is that as soon as we defined an [[Development:Blocks/Appendix_A#instance_allow_multiple.28.29| instance_allow_multiple()]] method, the method [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] that was already defined became obsolete. &lt;br /&gt;
&lt;br /&gt;
Moodle assumes that if a block allows multiple instances of itself, those instances will want to be configured (what is the point of same multiple instances in the same page if they are identical?) and thus automatically provides an &amp;quot;Edit&amp;quot; icon. So, we can also remove the whole [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] method now without harm. We had only needed it when multiple instances of the block were not allowed.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Effects of Globalization ==&lt;br /&gt;
&lt;br /&gt;
Configuring each block instance with its own personal data is cool enough, but sometimes administrators need some way to &amp;quot;touch&amp;quot; all instances of a specific block at the same time. In the case of our SimpleHTML block, a few settings that would make sense to apply to all instances aren&#039;t that hard to come up with. &lt;br /&gt;
&lt;br /&gt;
For example, we might want to limit the contents of each block to only so many characters, or we might have a setting that filters HTML out of the block&#039;s contents, only allowing pure text in. Granted, such a feature wouldn&#039;t win us any awards for naming our block &amp;quot;SimpleHTML&amp;quot; but some tormented administrator somewhere might actually find it useful.&lt;br /&gt;
&lt;br /&gt;
This kind of configuration is called &amp;quot;global configuration&amp;quot; and applies only to a specific block type (all instances of that block type are affected, however). Implementing such configuration for our block is quite similar to implementing the instance configuration. We will now see how to implement the second example, having a setting that only allows text and not HTML in the block&#039;s contents.&lt;br /&gt;
First of all, we need to tell Moodle that we want our block to provide global configuration by, what a surprise, adding a small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function has_config() {&lt;br /&gt;
  return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we need to create a HTML file that actually prints out the configuration screen. In our case, we &#039;ll just print out a checkbox saying &amp;quot;Do not allow HTML in the content&amp;quot; and a &amp;quot;submit&amp;quot; button. Let&#039;s create the file &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_global.html&amp;lt;/span&amp;gt; which again must be named just so, and copy paste the following into it:&lt;br /&gt;
&lt;br /&gt;
[[Development_talk:Blocks|TODO: New settings.php method]] &lt;br /&gt;
: Just to note that general documentation about admin settings is at [[Development:Admin_settings#Individual_settings]]. In the absence of documentation, you can look at blocks/course_list, blocks/online_users and blocks/rss_client. They all use a settings.php file.--[[User:Tim Hunt|Tim Hunt]] 19:38, 28 January 2009 (CST)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;1&amp;quot;&lt;br /&gt;
   &amp;lt;?php if(!empty($CFG-&amp;gt;block_simplehtml_strict)) &lt;br /&gt;
             echo &#039;checked=&amp;quot;checked&amp;quot;&#039;; ?&amp;gt; /&amp;gt;&lt;br /&gt;
   &amp;lt;?php print_string(&#039;donotallowhtml&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;&lt;br /&gt;
 &amp;lt;p&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;); ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
True to our block&#039;s name, this looks simple enough. What it does is that it displays a checkbox named &amp;quot;block_simplehtml_strict&amp;quot; and if the Moodle configuration variable with the same name (i.e., $CFG-&amp;gt;block_simplehtml_strict) is set and not empty (that means it&#039;s not equal to an empty string, to zero, or to boolean FALSE) it displays the box as pre-checked (reflecting the current status). &lt;br /&gt;
&lt;br /&gt;
Why does it check the configuration setting with the same name? Because the default implementation of the global configuration saving code takes all the variables we have in our form and saves them as Moodle configuration options with the same name. Thus, it&#039;s good practice to use a descriptive name and also one that won&#039;t possibly conflict with the name of another setting. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;block_simplehtml_strict&amp;quot; clearly satisfies both requirements.&lt;br /&gt;
&lt;br /&gt;
The astute reader may have noticed that we actually have &#039;&#039;two&#039;&#039; input fields named &amp;quot;block_simplehtml_strict&amp;quot; in our configuration file. One is hidden and its value is always 0; the other is the checkbox and its value is 1. What gives? Why have them both there?&lt;br /&gt;
&lt;br /&gt;
Actually, this is a small trick we use to make our job as simple as possible. HTML forms work this way: if a checkbox in a form is not checked, its name does not appear at all in the variables passed to PHP when the form is submitted. That effectively means that, when we uncheck the box and click submit, the variable is not passed to PHP at all. Thus, PHP does not know to update its value to &amp;quot;0&amp;quot;, and our &amp;quot;strict&amp;quot; setting cannot be turned off at all once we turn it on for the first time. Not the behavior we want, surely.&lt;br /&gt;
&lt;br /&gt;
However, when PHP handles received variables from a form, the variables are processed in the order in which they appear in the form. If a variable comes up having the same name with an already-processed variable, the new value overwrites the old one. Taking advantage of this, our logic runs as follows: the variable &amp;quot;block_simplehtml_strict&amp;quot; is first unconditionally set to &amp;quot;0&amp;quot;. Then, &#039;&#039;if&#039;&#039; the box is checked, it is set to &amp;quot;1&amp;quot;, overwriting the previous value as discussed. The net result is that our configuration setting behaves as it should.&lt;br /&gt;
&lt;br /&gt;
To round our bag of tricks up, notice that the use of &#039;&#039;if(!empty($CFG-&amp;gt;block_simplehtml_strict))&#039;&#039; in the test for &amp;quot;should the box be checked by default?&amp;quot; is quite deliberate. The first time this script runs, the variable &#039;&#039;&#039;$CFG-&amp;gt;block_simplehtml_strict&#039;&#039;&#039; will not exist at all. After it&#039;s set for the first time, its value can be either &amp;quot;0&amp;quot; or &amp;quot;1&amp;quot;. Given that both &amp;quot;not set&amp;quot; and the string &amp;quot;0&amp;quot; evaluate as empty while the sting &amp;quot;1&amp;quot; does not, we manage to avoid any warnings from PHP regarding the variable not being set at all, &#039;&#039;and&#039;&#039; have a nice human-readable representation for its two possible values (&amp;quot;0&amp;quot; and &amp;quot;1&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
=== config_save() ===&lt;br /&gt;
&lt;br /&gt;
Now that we have managed to cram a respectable amount of tricks into a few lines of HTML, we might as well discuss the alternative in case that tricks are not enough for a specific configuration setup we have in mind. Saving the data is done in the method [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], the default implementation of which is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  // Default behavior: save all variables as $CFG properties&lt;br /&gt;
  foreach ($data as $name =&amp;gt; $value) {&lt;br /&gt;
    set_config($name, $value);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As can be clearly seen, Moodle passes this method an associative array $data which contains all the variables coming in from our configuration screen. If we wanted to do the job without the &amp;quot;hidden variable with the same name&amp;quot; trick we used above, one way to do it would be by overriding this method with the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  if(isset($data[&#039;block_simplehtml_strict&#039;])) {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;1&#039;);&lt;br /&gt;
  }else {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;0&#039;);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quite straightfoward: if the variable &amp;quot;block_simplehtml_strict&amp;quot; is passed to us, then it can only mean that the user has checked it, so set the configuration variable with the same name to &amp;quot;1&amp;quot;. Otherwise, set it to &amp;quot;0&amp;quot;. Of course, this version would need to be updated if we add more configuration options because it doesn&#039;t respond to them as the default implementation does. Still, it&#039;s useful to know how we can override the default implementation if it does not fit our needs (for example, we might not want to save the variable as part of the Moodle configuration but do something else with it).&lt;br /&gt;
&lt;br /&gt;
So, we are now at the point where we know if the block should allow HTML tags in its content or not. How do we get the block to actually respect that setting?&lt;br /&gt;
&lt;br /&gt;
We could decide to do one of two things: either have the block &amp;quot;clean&amp;quot; HTML out from the input before saving it in the instance configuration and then display it as-is (the &amp;quot;eager&amp;quot; approach); or have it save the data &amp;quot;as is&amp;quot; and then clean it up each time just before displaying it (the &amp;quot;lazy&amp;quot; approach). The eager approach involves doing work once when saving the configuration; the lazy approach means doing work each time the block is displayed and thus it promises to be worse performance-wise. We shall hence go with the eager approach.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
=== instance_config_save() ===&lt;br /&gt;
&lt;br /&gt;
Much as we did just before with overriding [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], what is needed here is overriding the method [[Development:Blocks/Appendix_A#instance_config_save.28.29| instance_config_save()]] which handles the instance configuration. The default implementation is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  $data = stripslashes_recursive($data);&lt;br /&gt;
  $this-&amp;gt;config = $data;&lt;br /&gt;
  return set_field(&#039;block_instance&#039;, &lt;br /&gt;
                   &#039;configdata&#039;,&lt;br /&gt;
                    base64_encode(serialize($data)),&lt;br /&gt;
                   &#039;id&#039;, &lt;br /&gt;
                   $this-&amp;gt;instance-&amp;gt;id);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This may look intimidating at first (what&#039;s all this stripslashes_recursive() and base64_encode() and serialize() stuff?) but do not despair; we won&#039;t have to touch any of it. We will only add some extra validation code in the beginning and then instruct Moodle to additionally call this default implementation to do the actual storing of the data. Specifically, we will add a method to our class which goes like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  // Clean the data if we have to&lt;br /&gt;
  global $CFG;&lt;br /&gt;
  if(!empty($CFG-&amp;gt;block_simplehtml_strict)) {&lt;br /&gt;
    $data-&amp;gt;text = strip_tags($data-&amp;gt;text);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // And now forward to the default implementation defined in the parent class&lt;br /&gt;
  return parent::instance_config_save($data);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At last! Now the administrator has absolute power of life and death over what type of content is allowed in our &amp;quot;SimpleHTML&amp;quot; block! Absolute? Well... not exactly. In fact, if we think about it for a while, it will become apparent that if at some point in time HTML is allowed and some blocks have saved their content with HTML included, and afterwards the administrator changes the setting to &amp;quot;off&amp;quot;, this will only prevent subsequent content changes from including HTML. Blocks which already had HTML in their content would continue to display it!&lt;br /&gt;
&lt;br /&gt;
Following that train of thought, the next stop is realizing that we wouldn&#039;t have this problem if we had chosen the lazy approach a while back, because in that case we would &amp;quot;sanitize&amp;quot; each block&#039;s content just before it was displayed. &lt;br /&gt;
&lt;br /&gt;
The only thing we can do with the eager approach is strip all the tags from the content of all SimpleHTML instances as soon as the admin setting is changed to &amp;quot;HTML off&amp;quot;; but even then, turning the setting back to &amp;quot;HTML on&amp;quot; won&#039;t bring back the tags we stripped away. On the other hand, the lazy approach might be slower, but it&#039;s more versatile; we can choose whether to strip or keep the HTML before displaying the content, and we won&#039;t lose it at all if the admin toggles the setting off and on again. Isn&#039;t the life of a developer simple and wonderful?&lt;br /&gt;
&lt;br /&gt;
=== Exercise === &lt;br /&gt;
We will let this part of the tutorial come to a close with the obligatory exercise for the reader: &lt;br /&gt;
In order to have the SimpleHTML block work &amp;quot;correctly&amp;quot;, find out how to strengthen the eager approach to strip out all tags from the existing configuration of all instances of our block, &#039;&#039;&#039;or&#039;&#039;&#039; go back and implement the lazy approach instead. &lt;br /&gt;
(Hint: Do that in the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method.)&lt;br /&gt;
&lt;br /&gt;
=== UPDATING: === &lt;br /&gt;
Prior to version 1.5, the file &#039;&#039;config_global.html&#039;&#039; was named simply &#039;&#039;config.html&#039;&#039;. Also, the methods [[Blocks_Howto#method_config_save| config_save]] and [[Blocks_Howto#method_config_print| config_print]] were named &#039;&#039;&#039;handle_config&#039;&#039;&#039; and &#039;&#039;&#039;print_config&#039;&#039;&#039; respectively. Upgrading a block to work with Moodle 1.5 involves updating these aspects; refer to [[Blocks_Howto#appendix_b| Appendix B]] for more information.&lt;br /&gt;
&lt;br /&gt;
== Eye Candy ==&lt;br /&gt;
&lt;br /&gt;
Our block is just about complete functionally, so now let&#039;s take a look at some of the tricks we can use to make its behavior customized in a few more useful ways.&lt;br /&gt;
&lt;br /&gt;
First of all, there are a couple of ways we can adjust the visual aspects of our block. For starters, it might be useful to create a block that doesn&#039;t display a header (title) at all. You can see this effect in action in the Course Description block that comes with Moodle. This behavior is achieved by, you guessed it, adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function hide_header() {&lt;br /&gt;
  return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One more note here: we cannot just set an empty title inside the block&#039;s [[Development:Blocks/Appendix_A#init.28.29| init()]] method; it&#039;s necessary for each block to have a unique, non-empty title after [[Development:Blocks/Appendix_A#init.28.29| init()]] is called so that Moodle can use those titles to differentiate between all of the installed blocks.&lt;br /&gt;
&lt;br /&gt;
Another adjustment we might want to do is instruct our block to take up a certain amount of width on screen. Moodle handles this as a two-part process: first, it queries each block about its preferred width and takes the maximum number as the desired value. Then, the page that&#039;s being displayed can choose to use this value or, more probably, bring it within some specific range of values if it isn&#039;t already. That means that the width setting is a best-effort settlement; your block can &#039;&#039;request&#039;&#039; a certain width and Moodle will &#039;&#039;try&#039;&#039; to provide it, but there&#039;s no guarantee whatsoever about the end result. As a concrete example, all standard Moodle course formats will deliver any requested width between 180 and 210 pixels, inclusive.&lt;br /&gt;
&lt;br /&gt;
To instruct Moodle about our block&#039;s preferred width, we add one more method to the block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function preferred_width() {&lt;br /&gt;
  // The preferred value is in pixels&lt;br /&gt;
  return 200;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This will make our block (and all the other blocks displayed at the same side of the page) a bit wider than standard.&lt;br /&gt;
&lt;br /&gt;
Finally, we can also affect some properties of the actual HTML that will be used to print our block. Each block is fully contained within a &amp;amp;lt;table&amp;amp;gt; element, inside which all the HTML for that block is printed. We can instruct Moodle to add HTML attributes with specific values to that container. This would be done to either a) directly affect the end result (if we say, assign bgcolor=&amp;quot;black&amp;quot;), or b) give us freedom to customize the end result using CSS (this is in fact done by default as we &#039;ll see below).&lt;br /&gt;
&lt;br /&gt;
The default behavior of this feature in our case will assign to our block&#039;s container the class HTML attribute with the value &amp;quot;sideblock block_simplehtml&amp;quot; (the prefix &amp;quot;block_&amp;quot; followed by the name of our block, lowercased). We can then use that class to make CSS selectors in our theme to alter this block&#039;s visual style (for example, &amp;quot;.sideblock.block_simplehtml { border: 1px black solid}&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To change the default behavior, we will need to define a method which returns an associative array of attribute names and values. For example, the version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
  return array(&lt;br /&gt;
    &#039;class&#039;       =&amp;gt; &#039;sideblock block_&#039;. $this-&amp;gt;name(),&lt;br /&gt;
    &#039;onmouseover&#039; =&amp;gt; &amp;quot;alert(&#039;Mouseover on our block!&#039;);&amp;quot;&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will result in a mouseover event being added to our block using JavaScript, just as if we had written the onmouseover=&amp;quot;alert(...)&amp;quot; part ourselves in HTML. Note that we actually duplicate the part which sets the class attribute (we want to keep that, and since we override the default behavior it&#039;s our responsibility to emulate it if required). &lt;br /&gt;
&lt;br /&gt;
And the final elegant touch is that we don&#039;t set the class to the hard-coded value &amp;quot;block_simplehtml&amp;quot; but instead use the [[Development:Blocks/Appendix_A#name.28.29| name()]] method to make it dynamically match our block&#039;s name.&lt;br /&gt;
&lt;br /&gt;
== Authorized Personnel Only ==&lt;br /&gt;
&lt;br /&gt;
It&#039;s not difficult to imagine a block which is very useful in some circumstances but it simply cannot be made meaningful in others. An example of this would be the &amp;quot;Social Activities&amp;quot; block which is indeed useful in a course with the social format, but doesn&#039;t do anything useful in a course with the weeks format. There should be some way of allowing the use of such blocks only where they are indeed meaningful, and not letting them confuse users if they are not.&lt;br /&gt;
&lt;br /&gt;
Moodle allows us to declare which course formats each block is allowed to be displayed in, and enforces these restrictions as set by the block developers at all times. The information is given to Moodle as a standard associative array, with each key corresponding to a page format and defining a boolean value (true/false) that declares whether the block should be allowed to appear in that page format.&lt;br /&gt;
&lt;br /&gt;
Notice the deliberate use of the term &#039;&#039;page&#039;&#039; instead of &#039;&#039;course&#039;&#039; in the above paragraph. This is because in Moodle 1.5 and onwards, blocks can be displayed in any page that supports them. The best example of such pages are the course pages, but we are not restricted to them. For instance, the quiz view page (the first one we see when we click on the name of the quiz) also supports blocks.&lt;br /&gt;
&lt;br /&gt;
The format names we can use for the pages derive from the name of the script which is actually used to display that page. For example, when we are looking at a course, the script is &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/course/view.php&amp;lt;/span&amp;gt; (this is evident from the browser&#039;s address line). Thus, the format name of that page is &#039;&#039;&#039;course-view&#039;&#039;&#039;. It follows easily that the format name for a quiz view page is &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039;. This rule of thumb does have a few exceptions, however:&lt;br /&gt;
&lt;br /&gt;
# The format name for the front page of Moodle is &#039;&#039;&#039;site-index&#039;&#039;&#039;.&lt;br /&gt;
# The format name for courses is actually not just &#039;&#039;&#039;course-view&#039;&#039;&#039;&amp;lt;nowiki&amp;gt;; it is &amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;course-view-weeks&#039;&#039;&#039;, &#039;&#039;&#039;course-view-topics&#039;&#039;&#039;, etc.&lt;br /&gt;
# Even though there is no such page, the format name &#039;&#039;&#039;all&#039;&#039;&#039; can be used as a catch-all option.&lt;br /&gt;
&lt;br /&gt;
We can include as many format names as we want in our definition of the applicable formats. Each format can be allowed or disallowed, and there are also three more rules that help resolve the question &amp;quot;is this block allowed into this page or not?&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
# Prefixes of a format name will match that format name; for example, &#039;&#039;&#039;mod&#039;&#039;&#039; will match all the activity modules. &#039;&#039;&#039;course-view&#039;&#039;&#039; will match any course, regardless of the course format. And finally, &#039;&#039;&#039;site&#039;&#039;&#039; will also match the front page (remember that its full format name is &#039;&#039;&#039;site-index&#039;&#039;&#039;).&lt;br /&gt;
# The more specialized a format name that matches our page is, the higher precedence it has when deciding if the block will be allowed. For example, &#039;&#039;&#039;mod&#039;&#039;&#039;, &#039;&#039;&#039;mod-quiz&#039;&#039;&#039; and &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; all match the quiz view page. But if all three are present, &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; will take precedence over the other two because it is a better match.&lt;br /&gt;
# The character &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; can be used in place of any word. For example, &#039;&#039;&#039;mod&#039;&#039;&#039; and &#039;&#039;&#039;mod-*&#039;&#039;&#039; are equivalent. At the time of this document&#039;s writing, there is no actual reason to utilize this &amp;quot;wildcard matching&amp;quot; feature, but it exists for future usage.&lt;br /&gt;
# The order that the format names appear does not make any difference.&lt;br /&gt;
All of the above are enough to make the situation sound complex, so let&#039;s look at some specific examples. First of all, to have our block appear &#039;&#039;&#039;only&#039;&#039;&#039; in the site front page, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&#039;site&#039; =&amp;gt; TRUE);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Since &#039;&#039;&#039;all&#039;&#039;&#039; is missing, the block is disallowed from appearing in &#039;&#039;any&#039;&#039; course format; but then &#039;&#039;&#039;site&#039;&#039;&#039; is set to TRUE, so it&#039;s explicitly allowed to appear in the site front page (remember that &#039;&#039;&#039;site&#039;&#039;&#039; matches &#039;&#039;&#039;site-index&#039;&#039;&#039; because it&#039;s a prefix).&lt;br /&gt;
&lt;br /&gt;
For another example, if we wanted to allow the block to appear in all course formats &#039;&#039;except&#039;&#039; social, and also to &#039;&#039;not&#039;&#039; be allowed anywhere but in courses, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;course-view&#039; =&amp;gt; TRUE, &lt;br /&gt;
    &#039;course-view-social&#039; =&amp;gt; FALSE);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This time, we first allow the block to appear in all courses and then we explicitly disallow the social format.&lt;br /&gt;
For our final, most complicated example, suppose that a block can be displayed in the site front page, in courses (but not social courses) and also when we are viewing any activity module, &#039;&#039;except&#039;&#039; quiz. This would be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;site-index&#039; =&amp;gt; TRUE,&lt;br /&gt;
          &#039;course-view&#039; =&amp;gt; TRUE, &lt;br /&gt;
   &#039;course-view-social&#039; =&amp;gt; FALSE,&lt;br /&gt;
                  &#039;mod&#039; =&amp;gt; TRUE, &lt;br /&gt;
             &#039;mod-quiz&#039; =&amp;gt; FALSE&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is not difficult to realize that the above accomplishes the objective if we remember that there is a &amp;quot;best match&amp;quot; policy to determine the end result.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039; &amp;lt;br /&amp;gt;&lt;br /&gt;
Prior to version 1.5, blocks were only allowed in courses (and in Moodle 1.4, in the site front page). Also, the keywords used to describe the valid course formats at the time were slightly different and had to be changed in order to allow for a more open architecture. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== Lists and Icons ==&lt;br /&gt;
&lt;br /&gt;
In this final part of the guide we will briefly discuss an additional capability of Moodle&#039;s block system, namely the ability to very easily create blocks that display a list of choices to the user. This list is displayed with one item per line, and an optional image (icon) next to the item. An example of such a &#039;&#039;list block&#039;&#039; is the standard Moodle &amp;quot;admin&amp;quot; block, which illustrates all the points discussed in this section.&lt;br /&gt;
&lt;br /&gt;
As we have seen so far, blocks use two properties of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]]: &amp;quot;text&amp;quot; and &amp;quot;footer&amp;quot;. The text is displayed as-is as the block content, and the footer is displayed below the content in a smaller font size. List blocks use $this-&amp;gt;content-&amp;gt;footer in the exact same way, but they ignore $this-&amp;gt;content-&amp;gt;text.&lt;br /&gt;
&lt;br /&gt;
Instead, Moodle expects such blocks to set two other properties when the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called: $this-&amp;gt;content-&amp;gt;items and $this-&amp;gt;content-&amp;gt;icons. $this-&amp;gt;content-&amp;gt;items should be a numerically indexed array containing elements that represent the HTML for each item in the list that is going to be displayed. Usually these items will be HTML anchor tags which provide links to some page. $this-&amp;gt;content-&amp;gt;icons should also be a numerically indexed array, with exactly as many items as $this-&amp;gt;content-&amp;gt;items has. Each of these items should be a fully qualified HTML &amp;lt;img&amp;gt; tag, with &amp;quot;src&amp;quot;, &amp;quot;height&amp;quot;, &amp;quot;width&amp;quot; and &amp;quot;alt&amp;quot; attributes. Obviously, it makes sense to keep the images small and of a uniform size.&lt;br /&gt;
&lt;br /&gt;
In order to tell Moodle that we want to have a list block instead of the standard text block, we need to make a small change to our block class declaration. Instead of extending class &#039;&#039;&#039;block_base&#039;&#039;&#039;, our block will extend class &#039;&#039;&#039;block_list&#039;&#039;&#039;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
 class block_my_menu extends block_list {&lt;br /&gt;
     // The init() method does not need to change at all&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to making this change, we must of course also modify the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method to construct the [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] variable as discussed above:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function get_content() {&lt;br /&gt;
  if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content         = new stdClass;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items[] = &#039;&amp;lt;a href=&amp;quot;some_file.php&amp;quot;&amp;gt;Menu Option 1&amp;lt;/a&amp;gt;&#039;;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons[] = &#039;&amp;lt;img src=&amp;quot;images/icons/1.gif&amp;quot; class=&amp;quot;icon&amp;quot; alt=&amp;quot;&amp;quot; /&amp;gt;&#039;;&lt;br /&gt;
 &lt;br /&gt;
  // Add more list items here&lt;br /&gt;
 &lt;br /&gt;
  return $this-&amp;gt;content;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To summarize, if we want to create a list block instead of a text block, we just need to change the block class declaration and the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method. Adding the mandatory [[Development:Blocks/Appendix_A#init.28.29| init()]] method as discussed earlier will then give us our first list block in no time!&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== Appendices ==&lt;br /&gt;
&lt;br /&gt;
The appendices have been moved to separate pages:&lt;br /&gt;
&lt;br /&gt;
* Appendix A: [[Development:Blocks/Appendix A|&#039;&#039;block_base&#039;&#039; Reference]] &lt;br /&gt;
* Appendix B: [[Development:Blocks/Appendix B|Differences in the Blocks API for Moodle Versions prior to 1.5]]&lt;br /&gt;
* Appendix C: [[Development:Blocks/Appendix C|Creating Database Tables for Blocks (prior to 1.7)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Blocks]]&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
&lt;br /&gt;
[[es:Desarrollo de bloques]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55111</id>
		<title>Broken/Blocks</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55111"/>
		<updated>2009-04-30T10:36:26Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Configure That Out */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; A Step-by-step Guide To Creating Blocks &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Original Author: Jon Papaioannou (pj@moodle.org)&lt;br /&gt;
&lt;br /&gt;
The present document serves as a guide to developers who want to create their own blocks for use in Moodle. It applies to the 1.5 development version of Moodle (and any newer) &#039;&#039;&#039;only&#039;&#039;&#039;, as the blocks subsystem was rewritten and expanded for the 1.5 release. However, you can also find it useful if you want to modify blocks written for Moodle 1.3 and 1.4 to work with the latest versions (look at [[Development:Blocks/Appendix_B| Appendix B]]).&lt;br /&gt;
&lt;br /&gt;
The guide is written as an interactive course which aims to develop a configurable, multi-purpose block that displays arbitrary HTML. It&#039;s targeted mainly at people with little experience with Moodle or programming in general and aims to show how easy it is to create new blocks for Moodle. A certain small amount of PHP programming knowledge is still required, though. &lt;br /&gt;
&lt;br /&gt;
Experienced developers and those who just want a reference text should refer to [[Development:Blocks/Appendix_A| Appendix A]] because the main guide has a rather low concentration of pure information in the text.&lt;br /&gt;
&lt;br /&gt;
== Basic Concepts ==&lt;br /&gt;
&lt;br /&gt;
Through this guide, we will be following the creation of an &amp;quot;HTML&amp;quot; block from scratch in order to demonstrate most of the block features at our disposal. Our block will be named &amp;quot;SimpleHTML&amp;quot;. This does not constrain us regarding the name of the actual directory on the server where the files for our block will be stored, but for consistency we will follow the practice of using the lowercased form &amp;quot;simplehtml&amp;quot; in any case where such a name is required. &lt;br /&gt;
&lt;br /&gt;
Whenever we refer to a file or directory name which contains &amp;quot;simplehtml&amp;quot;, it&#039;s important to remember that &#039;&#039;only&#039;&#039; the &amp;quot;simplehtml&amp;quot; part is up to us to change; the rest is standardized and essential for Moodle to work correctly.&lt;br /&gt;
&lt;br /&gt;
Whenever a file&#039;s path is mentioned in this guide, it will always start with a slash. This refers to the Moodle home directory; all files and directories will be referred to with respect to that directory.&lt;br /&gt;
&lt;br /&gt;
== Ready, Set, Go! ==&lt;br /&gt;
&lt;br /&gt;
To define a &amp;quot;block&amp;quot; in Moodle, in the most basic case we need to provide just one source code file. We start by creating the directory &#039;&#039;/blocks/simplehtml/&#039;&#039; and creating a file named &#039;&#039;/blocks/simplehtml/&#039;&#039;&#039;&#039;&#039;block_simplehtml.php&#039;&#039;&#039; which will hold our code. We then begin coding the block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class block_simplehtml extends block_base {&lt;br /&gt;
  function init() {&lt;br /&gt;
    $this-&amp;gt;title   = get_string(&#039;simplehtml&#039;, &#039;block_simplehtml&#039;);&lt;br /&gt;
    $this-&amp;gt;version = 2004111200;&lt;br /&gt;
  }&lt;br /&gt;
  // The PHP tag and the curly bracket for the class definition &lt;br /&gt;
  // will only be closed after there is another function added in the next section.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first line is our block class definition; it must be named exactly in the manner shown. Again, only the &amp;quot;simplehtml&amp;quot; part can (and indeed must) change; everything else is standardized.&lt;br /&gt;
&lt;br /&gt;
Our class is then given a small method: [[Development:Blocks/Appendix_A#init.28.29| init()]]. This is essential for all blocks, and its purpose is to set the two class member variables listed inside it. But what do these values actually mean? Here&#039;s a more detailed description.&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Etitle| $this-&amp;gt;title]] is the title displayed in the header of our block. We can set it to whatever we like; in this case it&#039;s set to read the actual title from a language file we are presumably distributing together with the block. I &#039;ll skip ahead a bit here and say that if you want your block to display &#039;&#039;&#039;no&#039;&#039;&#039; title at all, then you should set this to any descriptive value you want (but &#039;&#039;&#039;not&#039;&#039;&#039; make it an empty string). We will later see [[Development:Blocks#Eye_Candy| how to disable the title&#039;s display]].&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] is the version of our block. This actually would only make a difference if your block wanted to keep its own data in special tables in the database (i.e. for very complex blocks). In that case the version number is used exactly as it&#039;s used in activities; an upgrade script uses it to incrementally upgrade an &amp;quot;old&amp;quot; version of the block&#039;s data to the latest. We will outline this process further ahead, since blocks tend to be relatively simple and not hold their own private data. &lt;br /&gt;
&lt;br /&gt;
In our example, this is certainly the case so we just set [[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] to &#039;&#039;&#039;YYYYMMDD00&#039;&#039;&#039; and forget about it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039;&amp;lt;br /&amp;gt; &lt;br /&gt;
Prior to version 1.5, the basic structure of each block class was slightly different. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== I Just Hear Static ==&lt;br /&gt;
In order to get our block to actually display something on screen, we need to add one more method to our class (before the final closing brace in our file). The new code is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
  function get_content() {&lt;br /&gt;
    if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
      return $this-&amp;gt;content;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;content         =  new stdClass;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
}   // Here&#039;s the closing curly bracket for the class definition&lt;br /&gt;
    // and here&#039;s the closing PHP tag from the section above.&lt;br /&gt;
?&amp;gt;  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can&#039;t get any simpler than that, can it? Let&#039;s dissect this method to see what&#039;s going on...&lt;br /&gt;
&lt;br /&gt;
First of all, there is a check that returns the current value of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] if it&#039;s not NULL; otherwise we proceed with &amp;quot;computing&amp;quot; it. Since the computation is potentially a time-consuming operation and it &#039;&#039;&#039;will&#039;&#039;&#039; be called several times for each block (Moodle works that way internally), we take a precaution and include this time-saver.&lt;br /&gt;
Supposing the content had not been computed before (it was NULL), we then define it from scratch. The code speaks for itself there, so there isn&#039;t much to say. Just keep in mind that we can use HTML both in the text &#039;&#039;&#039;and&#039;&#039;&#039; in the footer, if we want to.&lt;br /&gt;
&lt;br /&gt;
At this point our block should be capable of being automatically installed in Moodle and added to courses; visit your administration page to install it (Click &amp;quot;Notifications&amp;quot; under the Site Administration Block) and after seeing it in action come back to continue our tutorial.&lt;br /&gt;
&lt;br /&gt;
== Configure That Out ==&lt;br /&gt;
&lt;br /&gt;
The current version of our block doesn&#039;t really do much; it just displays a fixed message, which is not very useful. What we &#039;d really like to do is allow the teachers to customize what goes into the block. This, in block-speak, is called &amp;quot;instance configuration&amp;quot;. So let&#039;s give our block some instance configuration...&lt;br /&gt;
First of all, we need to tell Moodle that we want it to provide instance-specific configuration amenities to our block. That&#039;s as simple as adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_config() {&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This small change is enough to make Moodle display an &amp;quot;Edit...&amp;quot; icon in our block&#039;s header when we turn editing mode on in any course. However, if you try to click on that icon you will be presented with a notice that complains about the block&#039;s configuration not being implemented correctly. Try it, it&#039;s harmless.&lt;br /&gt;
Moodle&#039;s complaints do make sense. We told it that we want to have configuration, but we didn&#039;t say &#039;&#039;what&#039;&#039; kind of configuration we want, or how it should be displayed. To do that, we need to create one more file: &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/&#039;&#039;&#039;config_instance.html&#039;&#039;&#039;&amp;lt;/span&amp;gt; (which has to be named exactly like that). For the moment, copy paste the following into it and save:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;table cellpadding=&amp;quot;9&amp;quot; cellspacing=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_string(&#039;configcontent&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_textarea(true, 10, 50, 0, 0, &#039;text&#039;, $this-&amp;gt;config-&amp;gt;text); ?&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td colspan=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;) ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php use_html_editor(); ?&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It isn&#039;t difficult to see that the above code just provides us with a wysiwyg-editor-enabled textarea to write our block&#039;s desired content in and a submit button to save. But... what&#039;s $this-&amp;gt;config-&amp;gt;text? Well...&lt;br /&gt;
Moodle goes a long way to make things easier for block developers. Did you notice that the textarea is actually named &amp;quot;text&amp;quot;? When the submit button is pressed, Moodle saves each and every field it can find in our &#039;&#039;&#039;config_instance.html&#039;&#039;&#039; file as instance configuration data. &lt;br /&gt;
&lt;br /&gt;
We can then access that data as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;&#039;&#039;variablename&#039;&#039;&#039;&#039;&#039;, where &#039;&#039;variablename&#039;&#039; is the actual name we used for our field; in this case, &amp;quot;text&amp;quot;. So in essence, the above form just pre-populates the textarea with the current content of the block (as indeed it should) and then allows us to change it.&lt;br /&gt;
&lt;br /&gt;
You also might be surprised by the presence of a submit button and the absence of any &amp;lt;form&amp;gt; element at the same time. But the truth is, we don&#039;t need to worry about that at all; Moodle goes a really long way to make things easier for developers! We just print the configuration options we want, in any format we want; include a submit button, and Moodle will handle all the rest itself. The instance configuration variables are automatically at our disposal to access from any of the class methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29| init()]].&lt;br /&gt;
&lt;br /&gt;
In the event where the default behavior is not satisfactory, we can still override it. However, this requires advanced modifications to our block class and will not be covered here; refer to [[Development:Blocks/Appendix_A| Appendix A]] for more details.&lt;br /&gt;
Having now the ability to refer to this instance configuration data through [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]], the final twist is to tell our block to actually &#039;&#039;display&#039;&#039; what is saved in its configuration data. To do that, find this snippet in &#039;&#039;/blocks/simplehtml/block_simplehtml.php&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and change it to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Oh, and since the footer isn&#039;t really exciting at this point, we remove it from our block because it doesn&#039;t contribute anything. We could just as easily have decided to make the footer configurable in the above way, too. So for our latest code, the snippet becomes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After this discussion, our block is ready for prime time! Indeed, if you now visit any course with a SimpleHTML block, you will see that modifying its contents is now a snap.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Specialists ==&lt;br /&gt;
&lt;br /&gt;
Implementing instance configuration for the block&#039;s contents was good enough to whet our appetite, but who wants to stop there? Why not customize the block&#039;s title, too?&lt;br /&gt;
&lt;br /&gt;
Why not, indeed. Well, our first attempt to achieve this is natural enough: let&#039;s add another field to &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_instance.html&amp;lt;/span&amp;gt;. Here goes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&amp;lt;p&amp;gt;&lt;br /&gt;
    &amp;lt;?php print_string(&#039;configtitle&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;title&amp;quot; size=&amp;quot;30&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;config-&amp;gt;title; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We save the edited file, go to a course, edit the title of the block and... nothing happens! The instance configuration is saved correctly, all right (editing it once more proves that) but it&#039;s not being displayed. All we get is just the simple &amp;quot;SimpleHTML&amp;quot; title.&lt;br /&gt;
&lt;br /&gt;
That&#039;s not too weird, if we think back a bit. Do you remember that [[Development:Blocks/Appendix_A#init.28.29|init()]] method, where we set [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]]? We didn&#039;t actually change its value from then, and [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] is definitely not the same as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;title&#039;&#039;&#039; (to Moodle, at least). What we need is a way to update [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] with the value in the instance configuration. But as we said a bit earlier, we can use [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]] in all methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29|init()]]! So what can we do?&lt;br /&gt;
&lt;br /&gt;
Let&#039;s pull out another ace from our sleeve, and add this small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function specialization() {&lt;br /&gt;
  if(!empty($this-&amp;gt;config-&amp;gt;title)){&lt;br /&gt;
    $this-&amp;gt;title = $this-&amp;gt;config-&amp;gt;title;&lt;br /&gt;
  }else{&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;title = &#039;Some title ...&#039;;&lt;br /&gt;
  }&lt;br /&gt;
  if(empty($this-&amp;gt;config-&amp;gt;text)){&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;text = &#039;Some text ...&#039;;&lt;br /&gt;
  }    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Aha, here&#039;s what we wanted to do all along! But what&#039;s going on with the [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method?&lt;br /&gt;
&lt;br /&gt;
This &amp;quot;magic&amp;quot; method has actually a very nice property: it&#039;s &#039;&#039;guaranteed&#039;&#039; to be automatically called by Moodle as soon as our instance configuration is loaded and available (that is, immediately after [[Development:Blocks/Appendix_A#init.28.29|init()]] is called). That means before the block&#039;s content is computed for the first time, and indeed before &#039;&#039;anything&#039;&#039; else is done with the block. Thus, providing a [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method is the natural choice for any configuration data that needs to be acted upon &amp;quot;as soon as possible&amp;quot;, as in this case.&lt;br /&gt;
&lt;br /&gt;
== Now You See Me, Now You Don&#039;t ==&lt;br /&gt;
&lt;br /&gt;
Now would be a good time to mention another nifty technique that can be used in blocks, and which comes in handy quite often. Specifically, it may be the case that our block will have something interesting to display some of the time; but in some other cases, it won&#039;t have anything useful to say. (An example here would be the &amp;quot;Recent Activity&amp;quot; block, in the case where no recent activity in fact exists. &lt;br /&gt;
&lt;br /&gt;
However in that case the block chooses to explicitly inform you of the lack of said activity, which is arguably useful). It would be nice, then, to be able to have our block &amp;quot;disappear&amp;quot; if it&#039;s not needed to display it.&lt;br /&gt;
&lt;br /&gt;
This is indeed possible, and the way to do it is to make sure that after the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called, the block is completely void of content. Specifically, &amp;quot;void of content&amp;quot; means that both $this-&amp;gt;content-&amp;gt;text and $this-&amp;gt;content-&amp;gt;footer are each equal to the empty string (&amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt;). Moodle performs this check by calling the block&#039;s [[Development:Blocks/Appendix_A#is_empty.28.29| is_empty()]] method, and if the block is indeed empty then it is not displayed at all.&lt;br /&gt;
&lt;br /&gt;
Note that the exact value of the block&#039;s title and the presence or absence of a [[Development:Blocks/Appendix_A#hide_header.28.29| hide_header()]] method do &#039;&#039;not&#039;&#039; affect this behavior. A block is considered empty if it has no content, irrespective of anything else.&lt;br /&gt;
&lt;br /&gt;
== We Are Legion ==&lt;br /&gt;
&lt;br /&gt;
Right now our block is fully configurable, both in title and content. It&#039;s so versatile, in fact, that we could make pretty much anything out of it. It would be really nice to be able to add multiple blocks of this type to a single course. And, as you might have guessed, doing that is as simple as adding another small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_multiple() {&lt;br /&gt;
  return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Moodle that it should allow any number of instances of the SimpleHTML block in any course. After saving the changes to our file, Moodle immediately allows us to add multiple copies of the block without further ado!&lt;br /&gt;
&lt;br /&gt;
There are a couple more of interesting points to note here. First of all, even if a block itself allows multiple instances in the same page, the administrator still has the option of disallowing such behavior. This setting can be set separately for each block from the Administration / Configuration / Blocks page.&lt;br /&gt;
&lt;br /&gt;
And finally, a nice detail is that as soon as we defined an [[Development:Blocks/Appendix_A#instance_allow_multiple.28.29| instance_allow_multiple()]] method, the method [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] that was already defined became obsolete. &lt;br /&gt;
&lt;br /&gt;
Moodle assumes that if a block allows multiple instances of itself, those instances will want to be configured (what is the point of same multiple instances in the same page if they are identical?) and thus automatically provides an &amp;quot;Edit&amp;quot; icon. So, we can also remove the whole [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] method now without harm. We had only needed it when multiple instances of the block were not allowed.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Effects of Globalization ==&lt;br /&gt;
&lt;br /&gt;
Configuring each block instance with its own personal data is cool enough, but sometimes administrators need some way to &amp;quot;touch&amp;quot; all instances of a specific block at the same time. In the case of our SimpleHTML block, a few settings that would make sense to apply to all instances aren&#039;t that hard to come up with. &lt;br /&gt;
&lt;br /&gt;
For example, we might want to limit the contents of each block to only so many characters, or we might have a setting that filters HTML out of the block&#039;s contents, only allowing pure text in. Granted, such a feature wouldn&#039;t win us any awards for naming our block &amp;quot;SimpleHTML&amp;quot; but some tormented administrator somewhere might actually find it useful.&lt;br /&gt;
&lt;br /&gt;
This kind of configuration is called &amp;quot;global configuration&amp;quot; and applies only to a specific block type (all instances of that block type are affected, however). Implementing such configuration for our block is quite similar to implementing the instance configuration. We will now see how to implement the second example, having a setting that only allows text and not HTML in the block&#039;s contents.&lt;br /&gt;
First of all, we need to tell Moodle that we want our block to provide global configuration by, what a surprise, adding a small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function has_config() {&lt;br /&gt;
  return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we need to create a HTML file that actually prints out the configuration screen. In our case, we &#039;ll just print out a checkbox saying &amp;quot;Do not allow HTML in the content&amp;quot; and a &amp;quot;submit&amp;quot; button. Let&#039;s create the file &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_global.html&amp;lt;/span&amp;gt; which again must be named just so, and copy paste the following into it:&lt;br /&gt;
&lt;br /&gt;
[[Development_talk:Blocks|TODO: New settings.php method]] &lt;br /&gt;
: Just to note that general documentation about admin settings is at [[Development:Admin_settings#Individual_settings]]. In the absence of documentation, you can look at blocks/course_list, blocks/online_users and blocks/rss_client. They all use a settings.php file.--[[User:Tim Hunt|Tim Hunt]] 19:38, 28 January 2009 (CST)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;1&amp;quot;&lt;br /&gt;
   &amp;lt;?php if(!empty($CFG-&amp;gt;block_simplehtml_strict)) &lt;br /&gt;
             echo &#039;checked=&amp;quot;checked&amp;quot;&#039;; ?&amp;gt; /&amp;gt;&lt;br /&gt;
   &amp;lt;?php print_string(&#039;donotallowhtml&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;&lt;br /&gt;
 &amp;lt;p&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;); ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
True to our block&#039;s name, this looks simple enough. What it does is that it displays a checkbox named &amp;quot;block_simplehtml_strict&amp;quot; and if the Moodle configuration variable with the same name (i.e., $CFG-&amp;gt;block_simplehtml_strict) is set and not empty (that means it&#039;s not equal to an empty string, to zero, or to boolean FALSE) it displays the box as pre-checked (reflecting the current status). &lt;br /&gt;
&lt;br /&gt;
Why does it check the configuration setting with the same name? Because the default implementation of the global configuration saving code takes all the variables we have in our form and saves them as Moodle configuration options with the same name. Thus, it&#039;s good practice to use a descriptive name and also one that won&#039;t possibly conflict with the name of another setting. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;block_simplehtml_strict&amp;quot; clearly satisfies both requirements.&lt;br /&gt;
&lt;br /&gt;
The astute reader may have noticed that we actually have &#039;&#039;two&#039;&#039; input fields named &amp;quot;block_simplehtml_strict&amp;quot; in our configuration file. One is hidden and its value is always 0; the other is the checkbox and its value is 1. What gives? Why have them both there?&lt;br /&gt;
&lt;br /&gt;
Actually, this is a small trick we use to make our job as simple as possible. HTML forms work this way: if a checkbox in a form is not checked, its name does not appear at all in the variables passed to PHP when the form is submitted. That effectively means that, when we uncheck the box and click submit, the variable is not passed to PHP at all. Thus, PHP does not know to update its value to &amp;quot;0&amp;quot;, and our &amp;quot;strict&amp;quot; setting cannot be turned off at all once we turn it on for the first time. Not the behavior we want, surely.&lt;br /&gt;
&lt;br /&gt;
However, when PHP handles received variables from a form, the variables are processed in the order in which they appear in the form. If a variable comes up having the same name with an already-processed variable, the new value overwrites the old one. Taking advantage of this, our logic runs as follows: the variable &amp;quot;block_simplehtml_strict&amp;quot; is first unconditionally set to &amp;quot;0&amp;quot;. Then, &#039;&#039;if&#039;&#039; the box is checked, it is set to &amp;quot;1&amp;quot;, overwriting the previous value as discussed. The net result is that our configuration setting behaves as it should.&lt;br /&gt;
&lt;br /&gt;
To round our bag of tricks up, notice that the use of &#039;&#039;if(!empty($CFG-&amp;gt;block_simplehtml_strict))&#039;&#039; in the test for &amp;quot;should the box be checked by default?&amp;quot; is quite deliberate. The first time this script runs, the variable &#039;&#039;&#039;$CFG-&amp;gt;block_simplehtml_strict&#039;&#039;&#039; will not exist at all. After it&#039;s set for the first time, its value can be either &amp;quot;0&amp;quot; or &amp;quot;1&amp;quot;. Given that both &amp;quot;not set&amp;quot; and the string &amp;quot;0&amp;quot; evaluate as empty while the sting &amp;quot;1&amp;quot; does not, we manage to avoid any warnings from PHP regarding the variable not being set at all, &#039;&#039;and&#039;&#039; have a nice human-readable representation for its two possible values (&amp;quot;0&amp;quot; and &amp;quot;1&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
=== config_save() ===&lt;br /&gt;
&lt;br /&gt;
Now that we have managed to cram a respectable amount of tricks into a few lines of HTML, we might as well discuss the alternative in case that tricks are not enough for a specific configuration setup we have in mind. Saving the data is done in the method [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], the default implementation of which is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  // Default behavior: save all variables as $CFG properties&lt;br /&gt;
  foreach ($data as $name =&amp;gt; $value) {&lt;br /&gt;
    set_config($name, $value);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As can be clearly seen, Moodle passes this method an associative array $data which contains all the variables coming in from our configuration screen. If we wanted to do the job without the &amp;quot;hidden variable with the same name&amp;quot; trick we used above, one way to do it would be by overriding this method with the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  if(isset($data[&#039;block_simplehtml_strict&#039;])) {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;1&#039;);&lt;br /&gt;
  }else {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;0&#039;);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quite straightfoward: if the variable &amp;quot;block_simplehtml_strict&amp;quot; is passed to us, then it can only mean that the user has checked it, so set the configuration variable with the same name to &amp;quot;1&amp;quot;. Otherwise, set it to &amp;quot;0&amp;quot;. Of course, this version would need to be updated if we add more configuration options because it doesn&#039;t respond to them as the default implementation does. Still, it&#039;s useful to know how we can override the default implementation if it does not fit our needs (for example, we might not want to save the variable as part of the Moodle configuration but do something else with it).&lt;br /&gt;
&lt;br /&gt;
So, we are now at the point where we know if the block should allow HTML tags in its content or not. How do we get the block to actually respect that setting?&lt;br /&gt;
&lt;br /&gt;
We could decide to do one of two things: either have the block &amp;quot;clean&amp;quot; HTML out from the input before saving it in the instance configuration and then display it as-is (the &amp;quot;eager&amp;quot; approach); or have it save the data &amp;quot;as is&amp;quot; and then clean it up each time just before displaying it (the &amp;quot;lazy&amp;quot; approach). The eager approach involves doing work once when saving the configuration; the lazy approach means doing work each time the block is displayed and thus it promises to be worse performance-wise. We shall hence go with the eager approach.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
=== instance_config_save() ===&lt;br /&gt;
&lt;br /&gt;
Much as we did just before with overriding [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], what is needed here is overriding the method [[Development:Blocks/Appendix_A#instance_config_save.28.29| instance_config_save()]] which handles the instance configuration. The default implementation is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  $data = stripslashes_recursive($data);&lt;br /&gt;
  $this-&amp;gt;config = $data;&lt;br /&gt;
  return set_field(&#039;block_instance&#039;, &lt;br /&gt;
                   &#039;configdata&#039;,&lt;br /&gt;
                    base64_encode(serialize($data)),&lt;br /&gt;
                   &#039;id&#039;, &lt;br /&gt;
                   $this-&amp;gt;instance-&amp;gt;id);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This may look intimidating at first (what&#039;s all this stripslashes_recursive() and base64_encode() and serialize() stuff?) but do not despair; we won&#039;t have to touch any of it. We will only add some extra validation code in the beginning and then instruct Moodle to additionally call this default implementation to do the actual storing of the data. Specifically, we will add a method to our class which goes like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  // Clean the data if we have to&lt;br /&gt;
  global $CFG;&lt;br /&gt;
  if(!empty($CFG-&amp;gt;block_simplehtml_strict)) {&lt;br /&gt;
    $data-&amp;gt;text = strip_tags($data-&amp;gt;text);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // And now forward to the default implementation defined in the parent class&lt;br /&gt;
  return parent::instance_config_save($data);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At last! Now the administrator has absolute power of life and death over what type of content is allowed in our &amp;quot;SimpleHTML&amp;quot; block! Absolute? Well... not exactly. In fact, if we think about it for a while, it will become apparent that if at some point in time HTML is allowed and some blocks have saved their content with HTML included, and afterwards the administrator changes the setting to &amp;quot;off&amp;quot;, this will only prevent subsequent content changes from including HTML. Blocks which already had HTML in their content would continue to display it!&lt;br /&gt;
&lt;br /&gt;
Following that train of thought, the next stop is realizing that we wouldn&#039;t have this problem if we had chosen the lazy approach a while back, because in that case we would &amp;quot;sanitize&amp;quot; each block&#039;s content just before it was displayed. &lt;br /&gt;
&lt;br /&gt;
The only thing we can do with the eager approach is strip all the tags from the content of all SimpleHTML instances as soon as the admin setting is changed to &amp;quot;HTML off&amp;quot;; but even then, turning the setting back to &amp;quot;HTML on&amp;quot; won&#039;t bring back the tags we stripped away. On the other hand, the lazy approach might be slower, but it&#039;s more versatile; we can choose whether to strip or keep the HTML before displaying the content, and we won&#039;t lose it at all if the admin toggles the setting off and on again. Isn&#039;t the life of a developer simple and wonderful?&lt;br /&gt;
&lt;br /&gt;
=== Exercise === &lt;br /&gt;
We will let this part of the tutorial come to a close with the obligatory exercise for the reader: &lt;br /&gt;
In order to have the SimpleHTML block work &amp;quot;correctly&amp;quot;, find out how to strengthen the eager approach to strip out all tags from the existing configuration of all instances of our block, &#039;&#039;&#039;or&#039;&#039;&#039; go back and implement the lazy approach instead. &lt;br /&gt;
(Hint: Do that in the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method.)&lt;br /&gt;
&lt;br /&gt;
=== UPDATING: === &lt;br /&gt;
Prior to version 1.5, the file &#039;&#039;config_global.html&#039;&#039; was named simply &#039;&#039;config.html&#039;&#039;. Also, the methods [[Blocks_Howto#method_config_save| config_save]] and [[Blocks_Howto#method_config_print| config_print]] were named &#039;&#039;&#039;handle_config&#039;&#039;&#039; and &#039;&#039;&#039;print_config&#039;&#039;&#039; respectively. Upgrading a block to work with Moodle 1.5 involves updating these aspects; refer to [[Blocks_Howto#appendix_b| Appendix B]] for more information.&lt;br /&gt;
&lt;br /&gt;
== Eye Candy ==&lt;br /&gt;
&lt;br /&gt;
Our block is just about complete functionally, so now let&#039;s take a look at some of the tricks we can use to make its behavior customized in a few more useful ways.&lt;br /&gt;
&lt;br /&gt;
First of all, there are a couple of ways we can adjust the visual aspects of our block. For starters, it might be useful to create a block that doesn&#039;t display a header (title) at all. You can see this effect in action in the Course Description block that comes with Moodle. This behavior is achieved by, you guessed it, adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function hide_header() {&lt;br /&gt;
  return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One more note here: we cannot just set an empty title inside the block&#039;s [[Development:Blocks/Appendix_A#init.28.29| init()]] method; it&#039;s necessary for each block to have a unique, non-empty title after [[Development:Blocks/Appendix_A#init.28.29| init()]] is called so that Moodle can use those titles to differentiate between all of the installed blocks.&lt;br /&gt;
&lt;br /&gt;
Another adjustment we might want to do is instruct our block to take up a certain amount of width on screen. Moodle handles this as a two-part process: first, it queries each block about its preferred width and takes the maximum number as the desired value. Then, the page that&#039;s being displayed can choose to use this value or, more probably, bring it within some specific range of values if it isn&#039;t already. That means that the width setting is a best-effort settlement; your block can &#039;&#039;request&#039;&#039; a certain width and Moodle will &#039;&#039;try&#039;&#039; to provide it, but there&#039;s no guarantee whatsoever about the end result. As a concrete example, all standard Moodle course formats will deliver any requested width between 180 and 210 pixels, inclusive.&lt;br /&gt;
&lt;br /&gt;
To instruct Moodle about our block&#039;s preferred width, we add one more method to the block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function preferred_width() {&lt;br /&gt;
  // The preferred value is in pixels&lt;br /&gt;
  return 200;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This will make our block (and all the other blocks displayed at the same side of the page) a bit wider than standard.&lt;br /&gt;
&lt;br /&gt;
Finally, we can also affect some properties of the actual HTML that will be used to print our block. Each block is fully contained within a &amp;amp;lt;table&amp;amp;gt; element, inside which all the HTML for that block is printed. We can instruct Moodle to add HTML attributes with specific values to that container. This would be done to either a) directly affect the end result (if we say, assign bgcolor=&amp;quot;black&amp;quot;), or b) give us freedom to customize the end result using CSS (this is in fact done by default as we &#039;ll see below).&lt;br /&gt;
&lt;br /&gt;
The default behavior of this feature in our case will assign to our block&#039;s container the class HTML attribute with the value &amp;quot;sideblock block_simplehtml&amp;quot; (the prefix &amp;quot;block_&amp;quot; followed by the name of our block, lowercased). We can then use that class to make CSS selectors in our theme to alter this block&#039;s visual style (for example, &amp;quot;.sideblock.block_simplehtml { border: 1px black solid}&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To change the default behavior, we will need to define a method which returns an associative array of attribute names and values. For example, the version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
  return array(&lt;br /&gt;
    &#039;class&#039;       =&amp;gt; &#039;sideblock block_&#039;. $this-&amp;gt;name(),&lt;br /&gt;
    &#039;onmouseover&#039; =&amp;gt; &amp;quot;alert(&#039;Mouseover on our block!&#039;);&amp;quot;&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will result in a mouseover event being added to our block using JavaScript, just as if we had written the onmouseover=&amp;quot;alert(...)&amp;quot; part ourselves in HTML. Note that we actually duplicate the part which sets the class attribute (we want to keep that, and since we override the default behavior it&#039;s our responsibility to emulate it if required). &lt;br /&gt;
&lt;br /&gt;
And the final elegant touch is that we don&#039;t set the class to the hard-coded value &amp;quot;block_simplehtml&amp;quot; but instead use the [[Development:Blocks/Appendix_A#name.28.29| name()]] method to make it dynamically match our block&#039;s name.&lt;br /&gt;
&lt;br /&gt;
== Authorized Personnel Only ==&lt;br /&gt;
&lt;br /&gt;
It&#039;s not difficult to imagine a block which is very useful in some circumstances but it simply cannot be made meaningful in others. An example of this would be the &amp;quot;Social Activities&amp;quot; block which is indeed useful in a course with the social format, but doesn&#039;t do anything useful in a course with the weeks format. There should be some way of allowing the use of such blocks only where they are indeed meaningful, and not letting them confuse users if they are not.&lt;br /&gt;
&lt;br /&gt;
Moodle allows us to declare which course formats each block is allowed to be displayed in, and enforces these restrictions as set by the block developers at all times. The information is given to Moodle as a standard associative array, with each key corresponding to a page format and defining a boolean value (true/false) that declares whether the block should be allowed to appear in that page format.&lt;br /&gt;
&lt;br /&gt;
Notice the deliberate use of the term &#039;&#039;page&#039;&#039; instead of &#039;&#039;course&#039;&#039; in the above paragraph. This is because in Moodle 1.5 and onwards, blocks can be displayed in any page that supports them. The best example of such pages are the course pages, but we are not restricted to them. For instance, the quiz view page (the first one we see when we click on the name of the quiz) also supports blocks.&lt;br /&gt;
&lt;br /&gt;
The format names we can use for the pages derive from the name of the script which is actually used to display that page. For example, when we are looking at a course, the script is &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/course/view.php&amp;lt;/span&amp;gt; (this is evident from the browser&#039;s address line). Thus, the format name of that page is &#039;&#039;&#039;course-view&#039;&#039;&#039;. It follows easily that the format name for a quiz view page is &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039;. This rule of thumb does have a few exceptions, however:&lt;br /&gt;
&lt;br /&gt;
# The format name for the front page of Moodle is &#039;&#039;&#039;site-index&#039;&#039;&#039;.&lt;br /&gt;
# The format name for courses is actually not just &#039;&#039;&#039;course-view&#039;&#039;&#039;&amp;lt;nowiki&amp;gt;; it is &amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;course-view-weeks&#039;&#039;&#039;, &#039;&#039;&#039;course-view-topics&#039;&#039;&#039;, etc.&lt;br /&gt;
# Even though there is no such page, the format name &#039;&#039;&#039;all&#039;&#039;&#039; can be used as a catch-all option.&lt;br /&gt;
&lt;br /&gt;
We can include as many format names as we want in our definition of the applicable formats. Each format can be allowed or disallowed, and there are also three more rules that help resolve the question &amp;quot;is this block allowed into this page or not?&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
# Prefixes of a format name will match that format name; for example, &#039;&#039;&#039;mod&#039;&#039;&#039; will match all the activity modules. &#039;&#039;&#039;course-view&#039;&#039;&#039; will match any course, regardless of the course format. And finally, &#039;&#039;&#039;site&#039;&#039;&#039; will also match the front page (remember that its full format name is &#039;&#039;&#039;site-index&#039;&#039;&#039;).&lt;br /&gt;
# The more specialized a format name that matches our page is, the higher precedence it has when deciding if the block will be allowed. For example, &#039;&#039;&#039;mod&#039;&#039;&#039;, &#039;&#039;&#039;mod-quiz&#039;&#039;&#039; and &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; all match the quiz view page. But if all three are present, &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; will take precedence over the other two because it is a better match.&lt;br /&gt;
# The character &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; can be used in place of any word. For example, &#039;&#039;&#039;mod&#039;&#039;&#039; and &#039;&#039;&#039;mod-*&#039;&#039;&#039; are equivalent. At the time of this document&#039;s writing, there is no actual reason to utilize this &amp;quot;wildcard matching&amp;quot; feature, but it exists for future usage.&lt;br /&gt;
# The order that the format names appear does not make any difference.&lt;br /&gt;
All of the above are enough to make the situation sound complex, so let&#039;s look at some specific examples. First of all, to have our block appear &#039;&#039;&#039;only&#039;&#039;&#039; in the site front page, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&#039;site&#039; =&amp;gt; TRUE);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Since &#039;&#039;&#039;all&#039;&#039;&#039; is missing, the block is disallowed from appearing in &#039;&#039;any&#039;&#039; course format; but then &#039;&#039;&#039;site&#039;&#039;&#039; is set to TRUE, so it&#039;s explicitly allowed to appear in the site front page (remember that &#039;&#039;&#039;site&#039;&#039;&#039; matches &#039;&#039;&#039;site-index&#039;&#039;&#039; because it&#039;s a prefix).&lt;br /&gt;
&lt;br /&gt;
For another example, if we wanted to allow the block to appear in all course formats &#039;&#039;except&#039;&#039; social, and also to &#039;&#039;not&#039;&#039; be allowed anywhere but in courses, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;course-view&#039; =&amp;gt; TRUE, &lt;br /&gt;
    &#039;course-view-social&#039; =&amp;gt; FALSE);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This time, we first allow the block to appear in all courses and then we explicitly disallow the social format.&lt;br /&gt;
For our final, most complicated example, suppose that a block can be displayed in the site front page, in courses (but not social courses) and also when we are viewing any activity module, &#039;&#039;except&#039;&#039; quiz. This would be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;site-index&#039; =&amp;gt; TRUE,&lt;br /&gt;
          &#039;course-view&#039; =&amp;gt; TRUE, &lt;br /&gt;
   &#039;course-view-social&#039; =&amp;gt; FALSE,&lt;br /&gt;
                  &#039;mod&#039; =&amp;gt; TRUE, &lt;br /&gt;
             &#039;mod-quiz&#039; =&amp;gt; FALSE&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is not difficult to realize that the above accomplishes the objective if we remember that there is a &amp;quot;best match&amp;quot; policy to determine the end result.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039; &amp;lt;br /&amp;gt;&lt;br /&gt;
Prior to version 1.5, blocks were only allowed in courses (and in Moodle 1.4, in the site front page). Also, the keywords used to describe the valid course formats at the time were slightly different and had to be changed in order to allow for a more open architecture. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== Lists and Icons ==&lt;br /&gt;
&lt;br /&gt;
In this final part of the guide we will briefly discuss an additional capability of Moodle&#039;s block system, namely the ability to very easily create blocks that display a list of choices to the user. This list is displayed with one item per line, and an optional image (icon) next to the item. An example of such a &#039;&#039;list block&#039;&#039; is the standard Moodle &amp;quot;admin&amp;quot; block, which illustrates all the points discussed in this section.&lt;br /&gt;
&lt;br /&gt;
As we have seen so far, blocks use two properties of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]]: &amp;quot;text&amp;quot; and &amp;quot;footer&amp;quot;. The text is displayed as-is as the block content, and the footer is displayed below the content in a smaller font size. List blocks use $this-&amp;gt;content-&amp;gt;footer in the exact same way, but they ignore $this-&amp;gt;content-&amp;gt;text.&lt;br /&gt;
&lt;br /&gt;
Instead, Moodle expects such blocks to set two other properties when the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called: $this-&amp;gt;content-&amp;gt;items and $this-&amp;gt;content-&amp;gt;icons. $this-&amp;gt;content-&amp;gt;items should be a numerically indexed array containing elements that represent the HTML for each item in the list that is going to be displayed. Usually these items will be HTML anchor tags which provide links to some page. $this-&amp;gt;content-&amp;gt;icons should also be a numerically indexed array, with exactly as many items as $this-&amp;gt;content-&amp;gt;items has. Each of these items should be a fully qualified HTML &amp;lt;img&amp;gt; tag, with &amp;quot;src&amp;quot;, &amp;quot;height&amp;quot;, &amp;quot;width&amp;quot; and &amp;quot;alt&amp;quot; attributes. Obviously, it makes sense to keep the images small and of a uniform size.&lt;br /&gt;
&lt;br /&gt;
In order to tell Moodle that we want to have a list block instead of the standard text block, we need to make a small change to our block class declaration. Instead of extending class &#039;&#039;&#039;block_base&#039;&#039;&#039;, our block will extend class &#039;&#039;&#039;block_list&#039;&#039;&#039;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
 class block_my_menu extends block_list {&lt;br /&gt;
     // The init() method does not need to change at all&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to making this change, we must of course also modify the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method to construct the [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] variable as discussed above:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function get_content() {&lt;br /&gt;
  if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content         = new stdClass;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items[] = &#039;&amp;lt;a href=&amp;quot;some_file.php&amp;quot;&amp;gt;Menu Option 1&amp;lt;/a&amp;gt;&#039;;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons[] = &#039;&amp;lt;img src=&amp;quot;images/icons/1.gif&amp;quot; class=&amp;quot;icon&amp;quot; alt=&amp;quot;&amp;quot; /&amp;gt;&#039;;&lt;br /&gt;
 &lt;br /&gt;
  // Add more list items here&lt;br /&gt;
 &lt;br /&gt;
  return $this-&amp;gt;content;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To summarize, if we want to create a list block instead of a text block, we just need to change the block class declaration and the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method. Adding the mandatory [[Development:Blocks/Appendix_A#init.28.29| init()]] method as discussed earlier will then give us our first list block in no time!&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== Appendices ==&lt;br /&gt;
&lt;br /&gt;
The appendices have been moved to separate pages:&lt;br /&gt;
&lt;br /&gt;
* Appendix A: [[Development:Blocks/Appendix A|&#039;&#039;block_base&#039;&#039; Reference]] &lt;br /&gt;
* Appendix B: [[Development:Blocks/Appendix B|Differences in the Blocks API for Moodle Versions prior to 1.5]]&lt;br /&gt;
* Appendix C: [[Development:Blocks/Appendix C|Creating Database Tables for Blocks (prior to 1.7)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Blocks]]&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
&lt;br /&gt;
[[es:Desarrollo de bloques]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55089</id>
		<title>Broken/Blocks</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=Broken/Blocks&amp;diff=55089"/>
		<updated>2009-04-30T09:21:13Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* config_save() */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039; A Step-by-step Guide To Creating Blocks &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Original Author: Jon Papaioannou (pj@moodle.org)&lt;br /&gt;
&lt;br /&gt;
The present document serves as a guide to developers who want to create their own blocks for use in Moodle. It applies to the 1.5 development version of Moodle (and any newer) &#039;&#039;&#039;only&#039;&#039;&#039;, as the blocks subsystem was rewritten and expanded for the 1.5 release. However, you can also find it useful if you want to modify blocks written for Moodle 1.3 and 1.4 to work with the latest versions (look at [[Development:Blocks/Appendix_B| Appendix B]]).&lt;br /&gt;
&lt;br /&gt;
The guide is written as an interactive course which aims to develop a configurable, multi-purpose block that displays arbitrary HTML. It&#039;s targeted mainly at people with little experience with Moodle or programming in general and aims to show how easy it is to create new blocks for Moodle. A certain small amount of PHP programming knowledge is still required, though. &lt;br /&gt;
&lt;br /&gt;
Experienced developers and those who just want a reference text should refer to [[Development:Blocks/Appendix_A| Appendix A]] because the main guide has a rather low concentration of pure information in the text.&lt;br /&gt;
&lt;br /&gt;
== Basic Concepts ==&lt;br /&gt;
&lt;br /&gt;
Through this guide, we will be following the creation of an &amp;quot;HTML&amp;quot; block from scratch in order to demonstrate most of the block features at our disposal. Our block will be named &amp;quot;SimpleHTML&amp;quot;. This does not constrain us regarding the name of the actual directory on the server where the files for our block will be stored, but for consistency we will follow the practice of using the lowercased form &amp;quot;simplehtml&amp;quot; in any case where such a name is required. &lt;br /&gt;
&lt;br /&gt;
Whenever we refer to a file or directory name which contains &amp;quot;simplehtml&amp;quot;, it&#039;s important to remember that &#039;&#039;only&#039;&#039; the &amp;quot;simplehtml&amp;quot; part is up to us to change; the rest is standardized and essential for Moodle to work correctly.&lt;br /&gt;
&lt;br /&gt;
Whenever a file&#039;s path is mentioned in this guide, it will always start with a slash. This refers to the Moodle home directory; all files and directories will be referred to with respect to that directory.&lt;br /&gt;
&lt;br /&gt;
== Ready, Set, Go! ==&lt;br /&gt;
&lt;br /&gt;
To define a &amp;quot;block&amp;quot; in Moodle, in the most basic case we need to provide just one source code file. We start by creating the directory &#039;&#039;/blocks/simplehtml/&#039;&#039; and creating a file named &#039;&#039;/blocks/simplehtml/&#039;&#039;&#039;&#039;&#039;block_simplehtml.php&#039;&#039;&#039; which will hold our code. We then begin coding the block:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class block_simplehtml extends block_base {&lt;br /&gt;
  function init() {&lt;br /&gt;
    $this-&amp;gt;title   = get_string(&#039;simplehtml&#039;, &#039;block_simplehtml&#039;);&lt;br /&gt;
    $this-&amp;gt;version = 2004111200;&lt;br /&gt;
  }&lt;br /&gt;
  // The PHP tag and the curly bracket for the class definition &lt;br /&gt;
  // will only be closed after there is another function added in the next section.&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first line is our block class definition; it must be named exactly in the manner shown. Again, only the &amp;quot;simplehtml&amp;quot; part can (and indeed must) change; everything else is standardized.&lt;br /&gt;
&lt;br /&gt;
Our class is then given a small method: [[Development:Blocks/Appendix_A#init.28.29| init()]]. This is essential for all blocks, and its purpose is to set the two class member variables listed inside it. But what do these values actually mean? Here&#039;s a more detailed description.&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Etitle| $this-&amp;gt;title]] is the title displayed in the header of our block. We can set it to whatever we like; in this case it&#039;s set to read the actual title from a language file we are presumably distributing together with the block. I &#039;ll skip ahead a bit here and say that if you want your block to display &#039;&#039;&#039;no&#039;&#039;&#039; title at all, then you should set this to any descriptive value you want (but &#039;&#039;&#039;not&#039;&#039;&#039; make it an empty string). We will later see [[Development:Blocks#Eye_Candy| how to disable the title&#039;s display]].&lt;br /&gt;
&lt;br /&gt;
[[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] is the version of our block. This actually would only make a difference if your block wanted to keep its own data in special tables in the database (i.e. for very complex blocks). In that case the version number is used exactly as it&#039;s used in activities; an upgrade script uses it to incrementally upgrade an &amp;quot;old&amp;quot; version of the block&#039;s data to the latest. We will outline this process further ahead, since blocks tend to be relatively simple and not hold their own private data. &lt;br /&gt;
&lt;br /&gt;
In our example, this is certainly the case so we just set [[Development:Blocks/Appendix_A#.24this-.3Eversion| $this-&amp;gt;version]] to &#039;&#039;&#039;YYYYMMDD00&#039;&#039;&#039; and forget about it.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039;&amp;lt;br /&amp;gt; &lt;br /&gt;
Prior to version 1.5, the basic structure of each block class was slightly different. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== I Just Hear Static ==&lt;br /&gt;
In order to get our block to actually display something on screen, we need to add one more method to our class (before the final closing brace in our file). The new code is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;  &lt;br /&gt;
  function get_content() {&lt;br /&gt;
    if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
      return $this-&amp;gt;content;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;content         =  new stdClass;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
    $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
}   // Here&#039;s the closing curly bracket for the class definition&lt;br /&gt;
    // and here&#039;s the closing PHP tag from the section above.&lt;br /&gt;
?&amp;gt;  &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can&#039;t get any simpler than that, can it? Let&#039;s dissect this method to see what&#039;s going on...&lt;br /&gt;
&lt;br /&gt;
First of all, there is a check that returns the current value of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] if it&#039;s not NULL; otherwise we proceed with &amp;quot;computing&amp;quot; it. Since the computation is potentially a time-consuming operation and it &#039;&#039;&#039;will&#039;&#039;&#039; be called several times for each block (Moodle works that way internally), we take a precaution and include this time-saver.&lt;br /&gt;
Supposing the content had not been computed before (it was NULL), we then define it from scratch. The code speaks for itself there, so there isn&#039;t much to say. Just keep in mind that we can use HTML both in the text &#039;&#039;&#039;and&#039;&#039;&#039; in the footer, if we want to.&lt;br /&gt;
&lt;br /&gt;
At this point our block should be capable of being automatically installed in Moodle and added to courses; visit your administration page to install it (Click &amp;quot;Notifications&amp;quot; under the Site Administration Block) and after seeing it in action come back to continue our tutorial.&lt;br /&gt;
&lt;br /&gt;
== Configure That Out ==&lt;br /&gt;
&lt;br /&gt;
The current version of our block doesn&#039;t really do much; it just displays a fixed message, which is not very useful. What we &#039;d really like to do is allow the teachers to customize what goes into the block. This, in block-speak, is called &amp;quot;instance configuration&amp;quot;. So let&#039;s give our block some instance configuration...&lt;br /&gt;
First of all, we need to tell Moodle that we want it to provide instance-specific configuration amenities to our block. That&#039;s as simple as adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_config() {&lt;br /&gt;
  return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This small change is enough to make Moodle display an &amp;quot;Edit...&amp;quot; icon in our block&#039;s header when we turn editing mode on in any course. However, if you try to click on that icon you will be presented with a notice that complains about the block&#039;s configuration not being implemented correctly. Try it, it&#039;s harmless.&lt;br /&gt;
Moodle&#039;s complaints do make sense. We told it that we want to have configuration, but we didn&#039;t say &#039;&#039;what&#039;&#039; kind of configuration we want, or how it should be displayed. To do that, we need to create one more file: &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/&#039;&#039;&#039;config_instance.html&#039;&#039;&#039;&amp;lt;/span&amp;gt; (which has to be named exactly like that). For the moment, copy paste the following into it and save:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;table cellpadding=&amp;quot;9&amp;quot; cellspacing=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_string(&#039;configcontent&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
    &amp;lt;td&amp;gt;&lt;br /&gt;
       &amp;lt;?php print_textarea(TRUE, 10, 50, 0, 0, &#039;text&#039;, $this-&amp;gt;config-&amp;gt;text); ?&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
  &amp;lt;tr&amp;gt;&lt;br /&gt;
    &amp;lt;td colspan=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;) ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php use_html_editor(); ?&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It isn&#039;t difficult to see that the above code just provides us with a wysiwyg-editor-enabled textarea to write our block&#039;s desired content in and a submit button to save. But... what&#039;s $this-&amp;gt;config-&amp;gt;text? Well...&lt;br /&gt;
Moodle goes a long way to make things easier for block developers. Did you notice that the textarea is actually named &amp;quot;text&amp;quot;? When the submit button is pressed, Moodle saves each and every field it can find in our &#039;&#039;&#039;config_instance.html&#039;&#039;&#039; file as instance configuration data. &lt;br /&gt;
&lt;br /&gt;
We can then access that data as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;&#039;&#039;variablename&#039;&#039;&#039;&#039;&#039;, where &#039;&#039;variablename&#039;&#039; is the actual name we used for our field; in this case, &amp;quot;text&amp;quot;. So in essence, the above form just pre-populates the textarea with the current content of the block (as indeed it should) and then allows us to change it.&lt;br /&gt;
&lt;br /&gt;
You also might be surprised by the presence of a submit button and the absence of any &amp;lt;form&amp;gt; element at the same time. But the truth is, we don&#039;t need to worry about that at all; Moodle goes a really long way to make things easier for developers! We just print the configuration options we want, in any format we want; include a submit button, and Moodle will handle all the rest itself. The instance configuration variables are automatically at our disposal to access from any of the class methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29| init()]].&lt;br /&gt;
&lt;br /&gt;
In the event where the default behavior is not satisfactory, we can still override it. However, this requires advanced modifications to our block class and will not be covered here; refer to [[Development:Blocks/Appendix_A| Appendix A]] for more details.&lt;br /&gt;
Having now the ability to refer to this instance configuration data through [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]], the final twist is to tell our block to actually &#039;&#039;display&#039;&#039; what is saved in its configuration data. To do that, find this snippet in &#039;&#039;/blocks/simplehtml/block_simplehtml.php&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = &#039;The content of our SimpleHTML block!&#039;;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and change it to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Oh, and since the footer isn&#039;t really exciting at this point, we remove it from our block because it doesn&#039;t contribute anything. We could just as easily have decided to make the footer configurable in the above way, too. So for our latest code, the snippet becomes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
 $this-&amp;gt;content = new stdClass;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;text   = $this-&amp;gt;config-&amp;gt;text;&lt;br /&gt;
 $this-&amp;gt;content-&amp;gt;footer = &#039;&#039;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After this discussion, our block is ready for prime time! Indeed, if you now visit any course with a SimpleHTML block, you will see that modifying its contents is now a snap.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Specialists ==&lt;br /&gt;
&lt;br /&gt;
Implementing instance configuration for the block&#039;s contents was good enough to whet our appetite, but who wants to stop there? Why not customize the block&#039;s title, too?&lt;br /&gt;
&lt;br /&gt;
Why not, indeed. Well, our first attempt to achieve this is natural enough: let&#039;s add another field to &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_instance.html&amp;lt;/span&amp;gt;. Here goes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;tr valign=&amp;quot;top&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;td align=&amp;quot;right&amp;quot;&amp;gt;&amp;lt;p&amp;gt;&lt;br /&gt;
    &amp;lt;?php print_string(&#039;configtitle&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;:&amp;lt;/p&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
  &amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;title&amp;quot; size=&amp;quot;30&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;config-&amp;gt;title; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We save the edited file, go to a course, edit the title of the block and... nothing happens! The instance configuration is saved correctly, all right (editing it once more proves that) but it&#039;s not being displayed. All we get is just the simple &amp;quot;SimpleHTML&amp;quot; title.&lt;br /&gt;
&lt;br /&gt;
That&#039;s not too weird, if we think back a bit. Do you remember that [[Development:Blocks/Appendix_A#init.28.29|init()]] method, where we set [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]]? We didn&#039;t actually change its value from then, and [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] is definitely not the same as &#039;&#039;&#039;$this-&amp;gt;config-&amp;gt;title&#039;&#039;&#039; (to Moodle, at least). What we need is a way to update [[Development:Blocks/Appendix_A#.24this-.3Etitle|$this-&amp;gt;title]] with the value in the instance configuration. But as we said a bit earlier, we can use [[Development:Blocks/Appendix_A#.24this-.3Econfig| $this-&amp;gt;config]] in all methods &#039;&#039;except&#039;&#039; [[Development:Blocks/Appendix_A#init.28.29|init()]]! So what can we do?&lt;br /&gt;
&lt;br /&gt;
Let&#039;s pull out another ace from our sleeve, and add this small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function specialization() {&lt;br /&gt;
  if(!empty($this-&amp;gt;config-&amp;gt;title)){&lt;br /&gt;
    $this-&amp;gt;title = $this-&amp;gt;config-&amp;gt;title;&lt;br /&gt;
  }else{&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;title = &#039;Some title ...&#039;;&lt;br /&gt;
  }&lt;br /&gt;
  if(empty($this-&amp;gt;config-&amp;gt;text)){&lt;br /&gt;
    $this-&amp;gt;config-&amp;gt;text = &#039;Some text ...&#039;;&lt;br /&gt;
  }    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Aha, here&#039;s what we wanted to do all along! But what&#039;s going on with the [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method?&lt;br /&gt;
&lt;br /&gt;
This &amp;quot;magic&amp;quot; method has actually a very nice property: it&#039;s &#039;&#039;guaranteed&#039;&#039; to be automatically called by Moodle as soon as our instance configuration is loaded and available (that is, immediately after [[Development:Blocks/Appendix_A#init.28.29|init()]] is called). That means before the block&#039;s content is computed for the first time, and indeed before &#039;&#039;anything&#039;&#039; else is done with the block. Thus, providing a [[Development:Blocks/Appendix_A#specialization.28.29| specialization()]] method is the natural choice for any configuration data that needs to be acted upon &amp;quot;as soon as possible&amp;quot;, as in this case.&lt;br /&gt;
&lt;br /&gt;
== Now You See Me, Now You Don&#039;t ==&lt;br /&gt;
&lt;br /&gt;
Now would be a good time to mention another nifty technique that can be used in blocks, and which comes in handy quite often. Specifically, it may be the case that our block will have something interesting to display some of the time; but in some other cases, it won&#039;t have anything useful to say. (An example here would be the &amp;quot;Recent Activity&amp;quot; block, in the case where no recent activity in fact exists. &lt;br /&gt;
&lt;br /&gt;
However in that case the block chooses to explicitly inform you of the lack of said activity, which is arguably useful). It would be nice, then, to be able to have our block &amp;quot;disappear&amp;quot; if it&#039;s not needed to display it.&lt;br /&gt;
&lt;br /&gt;
This is indeed possible, and the way to do it is to make sure that after the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called, the block is completely void of content. Specifically, &amp;quot;void of content&amp;quot; means that both $this-&amp;gt;content-&amp;gt;text and $this-&amp;gt;content-&amp;gt;footer are each equal to the empty string (&amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt;). Moodle performs this check by calling the block&#039;s [[Development:Blocks/Appendix_A#is_empty.28.29| is_empty()]] method, and if the block is indeed empty then it is not displayed at all.&lt;br /&gt;
&lt;br /&gt;
Note that the exact value of the block&#039;s title and the presence or absence of a [[Development:Blocks/Appendix_A#hide_header.28.29| hide_header()]] method do &#039;&#039;not&#039;&#039; affect this behavior. A block is considered empty if it has no content, irrespective of anything else.&lt;br /&gt;
&lt;br /&gt;
== We Are Legion ==&lt;br /&gt;
&lt;br /&gt;
Right now our block is fully configurable, both in title and content. It&#039;s so versatile, in fact, that we could make pretty much anything out of it. It would be really nice to be able to add multiple blocks of this type to a single course. And, as you might have guessed, doing that is as simple as adding another small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_allow_multiple() {&lt;br /&gt;
  return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This tells Moodle that it should allow any number of instances of the SimpleHTML block in any course. After saving the changes to our file, Moodle immediately allows us to add multiple copies of the block without further ado!&lt;br /&gt;
&lt;br /&gt;
There are a couple more of interesting points to note here. First of all, even if a block itself allows multiple instances in the same page, the administrator still has the option of disallowing such behavior. This setting can be set separately for each block from the Administration / Configuration / Blocks page.&lt;br /&gt;
&lt;br /&gt;
And finally, a nice detail is that as soon as we defined an [[Development:Blocks/Appendix_A#instance_allow_multiple.28.29| instance_allow_multiple()]] method, the method [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] that was already defined became obsolete. &lt;br /&gt;
&lt;br /&gt;
Moodle assumes that if a block allows multiple instances of itself, those instances will want to be configured (what is the point of same multiple instances in the same page if they are identical?) and thus automatically provides an &amp;quot;Edit&amp;quot; icon. So, we can also remove the whole [[Development:Blocks/Appendix_A#instance_allow_config.28.29| instance_allow_config()]] method now without harm. We had only needed it when multiple instances of the block were not allowed.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== The Effects of Globalization ==&lt;br /&gt;
&lt;br /&gt;
Configuring each block instance with its own personal data is cool enough, but sometimes administrators need some way to &amp;quot;touch&amp;quot; all instances of a specific block at the same time. In the case of our SimpleHTML block, a few settings that would make sense to apply to all instances aren&#039;t that hard to come up with. &lt;br /&gt;
&lt;br /&gt;
For example, we might want to limit the contents of each block to only so many characters, or we might have a setting that filters HTML out of the block&#039;s contents, only allowing pure text in. Granted, such a feature wouldn&#039;t win us any awards for naming our block &amp;quot;SimpleHTML&amp;quot; but some tormented administrator somewhere might actually find it useful.&lt;br /&gt;
&lt;br /&gt;
This kind of configuration is called &amp;quot;global configuration&amp;quot; and applies only to a specific block type (all instances of that block type are affected, however). Implementing such configuration for our block is quite similar to implementing the instance configuration. We will now see how to implement the second example, having a setting that only allows text and not HTML in the block&#039;s contents.&lt;br /&gt;
First of all, we need to tell Moodle that we want our block to provide global configuration by, what a surprise, adding a small method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function has_config() {&lt;br /&gt;
  return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, we need to create a HTML file that actually prints out the configuration screen. In our case, we &#039;ll just print out a checkbox saying &amp;quot;Do not allow HTML in the content&amp;quot; and a &amp;quot;submit&amp;quot; button. Let&#039;s create the file &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/blocks/simplehtml/config_global.html&amp;lt;/span&amp;gt; which again must be named just so, and copy paste the following into it:&lt;br /&gt;
&lt;br /&gt;
[[Development_talk:Blocks|TODO: New settings.php method]] &lt;br /&gt;
: Just to note that general documentation about admin settings is at [[Development:Admin_settings#Individual_settings]]. In the absence of documentation, you can look at blocks/course_list, blocks/online_users and blocks/rss_client. They all use a settings.php file.--[[User:Tim Hunt|Tim Hunt]] 19:38, 28 January 2009 (CST)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;text-align: center;&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;block_simplehtml_strict&amp;quot; value=&amp;quot;1&amp;quot;&lt;br /&gt;
   &amp;lt;?php if(!empty($CFG-&amp;gt;block_simplehtml_strict)) &lt;br /&gt;
             echo &#039;checked=&amp;quot;checked&amp;quot;&#039;; ?&amp;gt; /&amp;gt;&lt;br /&gt;
   &amp;lt;?php print_string(&#039;donotallowhtml&#039;, &#039;block_simplehtml&#039;); ?&amp;gt;&lt;br /&gt;
 &amp;lt;p&amp;gt;&lt;br /&gt;
 &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;&amp;lt;?php print_string(&#039;savechanges&#039;); ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
 &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
True to our block&#039;s name, this looks simple enough. What it does is that it displays a checkbox named &amp;quot;block_simplehtml_strict&amp;quot; and if the Moodle configuration variable with the same name (i.e., $CFG-&amp;gt;block_simplehtml_strict) is set and not empty (that means it&#039;s not equal to an empty string, to zero, or to boolean FALSE) it displays the box as pre-checked (reflecting the current status). &lt;br /&gt;
&lt;br /&gt;
Why does it check the configuration setting with the same name? Because the default implementation of the global configuration saving code takes all the variables we have in our form and saves them as Moodle configuration options with the same name. Thus, it&#039;s good practice to use a descriptive name and also one that won&#039;t possibly conflict with the name of another setting. &lt;br /&gt;
&lt;br /&gt;
&amp;quot;block_simplehtml_strict&amp;quot; clearly satisfies both requirements.&lt;br /&gt;
&lt;br /&gt;
The astute reader may have noticed that we actually have &#039;&#039;two&#039;&#039; input fields named &amp;quot;block_simplehtml_strict&amp;quot; in our configuration file. One is hidden and its value is always 0; the other is the checkbox and its value is 1. What gives? Why have them both there?&lt;br /&gt;
&lt;br /&gt;
Actually, this is a small trick we use to make our job as simple as possible. HTML forms work this way: if a checkbox in a form is not checked, its name does not appear at all in the variables passed to PHP when the form is submitted. That effectively means that, when we uncheck the box and click submit, the variable is not passed to PHP at all. Thus, PHP does not know to update its value to &amp;quot;0&amp;quot;, and our &amp;quot;strict&amp;quot; setting cannot be turned off at all once we turn it on for the first time. Not the behavior we want, surely.&lt;br /&gt;
&lt;br /&gt;
However, when PHP handles received variables from a form, the variables are processed in the order in which they appear in the form. If a variable comes up having the same name with an already-processed variable, the new value overwrites the old one. Taking advantage of this, our logic runs as follows: the variable &amp;quot;block_simplehtml_strict&amp;quot; is first unconditionally set to &amp;quot;0&amp;quot;. Then, &#039;&#039;if&#039;&#039; the box is checked, it is set to &amp;quot;1&amp;quot;, overwriting the previous value as discussed. The net result is that our configuration setting behaves as it should.&lt;br /&gt;
&lt;br /&gt;
To round our bag of tricks up, notice that the use of &#039;&#039;if(!empty($CFG-&amp;gt;block_simplehtml_strict))&#039;&#039; in the test for &amp;quot;should the box be checked by default?&amp;quot; is quite deliberate. The first time this script runs, the variable &#039;&#039;&#039;$CFG-&amp;gt;block_simplehtml_strict&#039;&#039;&#039; will not exist at all. After it&#039;s set for the first time, its value can be either &amp;quot;0&amp;quot; or &amp;quot;1&amp;quot;. Given that both &amp;quot;not set&amp;quot; and the string &amp;quot;0&amp;quot; evaluate as empty while the sting &amp;quot;1&amp;quot; does not, we manage to avoid any warnings from PHP regarding the variable not being set at all, &#039;&#039;and&#039;&#039; have a nice human-readable representation for its two possible values (&amp;quot;0&amp;quot; and &amp;quot;1&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
=== config_save() ===&lt;br /&gt;
&lt;br /&gt;
Now that we have managed to cram a respectable amount of tricks into a few lines of HTML, we might as well discuss the alternative in case that tricks are not enough for a specific configuration setup we have in mind. Saving the data is done in the method [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], the default implementation of which is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  // Default behavior: save all variables as $CFG properties&lt;br /&gt;
  foreach ($data as $name =&amp;gt; $value) {&lt;br /&gt;
    set_config($name, $value);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As can be clearly seen, Moodle passes this method an associative array $data which contains all the variables coming in from our configuration screen. If we wanted to do the job without the &amp;quot;hidden variable with the same name&amp;quot; trick we used above, one way to do it would be by overriding this method with the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function config_save($data) {&lt;br /&gt;
  if(isset($data[&#039;block_simplehtml_strict&#039;])) {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;1&#039;);&lt;br /&gt;
  }else {&lt;br /&gt;
    set_config(&#039;block_simplehtml_strict&#039;, &#039;0&#039;);&lt;br /&gt;
  }&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Quite straightfoward: if the variable &amp;quot;block_simplehtml_strict&amp;quot; is passed to us, then it can only mean that the user has checked it, so set the configuration variable with the same name to &amp;quot;1&amp;quot;. Otherwise, set it to &amp;quot;0&amp;quot;. Of course, this version would need to be updated if we add more configuration options because it doesn&#039;t respond to them as the default implementation does. Still, it&#039;s useful to know how we can override the default implementation if it does not fit our needs (for example, we might not want to save the variable as part of the Moodle configuration but do something else with it).&lt;br /&gt;
&lt;br /&gt;
So, we are now at the point where we know if the block should allow HTML tags in its content or not. How do we get the block to actually respect that setting?&lt;br /&gt;
&lt;br /&gt;
We could decide to do one of two things: either have the block &amp;quot;clean&amp;quot; HTML out from the input before saving it in the instance configuration and then display it as-is (the &amp;quot;eager&amp;quot; approach); or have it save the data &amp;quot;as is&amp;quot; and then clean it up each time just before displaying it (the &amp;quot;lazy&amp;quot; approach). The eager approach involves doing work once when saving the configuration; the lazy approach means doing work each time the block is displayed and thus it promises to be worse performance-wise. We shall hence go with the eager approach.&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
=== instance_config_save() ===&lt;br /&gt;
&lt;br /&gt;
Much as we did just before with overriding [[Development:Blocks/Appendix_A#config_save.28.29| config_save()]], what is needed here is overriding the method [[Development:Blocks/Appendix_A#instance_config_save.28.29| instance_config_save()]] which handles the instance configuration. The default implementation is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  $data = stripslashes_recursive($data);&lt;br /&gt;
  $this-&amp;gt;config = $data;&lt;br /&gt;
  return set_field(&#039;block_instance&#039;, &lt;br /&gt;
                   &#039;configdata&#039;,&lt;br /&gt;
                    base64_encode(serialize($data)),&lt;br /&gt;
                   &#039;id&#039;, &lt;br /&gt;
                   $this-&amp;gt;instance-&amp;gt;id);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This may look intimidating at first (what&#039;s all this stripslashes_recursive() and base64_encode() and serialize() stuff?) but do not despair; we won&#039;t have to touch any of it. We will only add some extra validation code in the beginning and then instruct Moodle to additionally call this default implementation to do the actual storing of the data. Specifically, we will add a method to our class which goes like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function instance_config_save($data) {&lt;br /&gt;
  // Clean the data if we have to&lt;br /&gt;
  global $CFG;&lt;br /&gt;
  if(!empty($CFG-&amp;gt;block_simplehtml_strict)) {&lt;br /&gt;
    $data-&amp;gt;text = strip_tags($data-&amp;gt;text);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // And now forward to the default implementation defined in the parent class&lt;br /&gt;
  return parent::instance_config_save($data);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At last! Now the administrator has absolute power of life and death over what type of content is allowed in our &amp;quot;SimpleHTML&amp;quot; block! Absolute? Well... not exactly. In fact, if we think about it for a while, it will become apparent that if at some point in time HTML is allowed and some blocks have saved their content with HTML included, and afterwards the administrator changes the setting to &amp;quot;off&amp;quot;, this will only prevent subsequent content changes from including HTML. Blocks which already had HTML in their content would continue to display it!&lt;br /&gt;
&lt;br /&gt;
Following that train of thought, the next stop is realizing that we wouldn&#039;t have this problem if we had chosen the lazy approach a while back, because in that case we would &amp;quot;sanitize&amp;quot; each block&#039;s content just before it was displayed. &lt;br /&gt;
&lt;br /&gt;
The only thing we can do with the eager approach is strip all the tags from the content of all SimpleHTML instances as soon as the admin setting is changed to &amp;quot;HTML off&amp;quot;; but even then, turning the setting back to &amp;quot;HTML on&amp;quot; won&#039;t bring back the tags we stripped away. On the other hand, the lazy approach might be slower, but it&#039;s more versatile; we can choose whether to strip or keep the HTML before displaying the content, and we won&#039;t lose it at all if the admin toggles the setting off and on again. Isn&#039;t the life of a developer simple and wonderful?&lt;br /&gt;
&lt;br /&gt;
=== Exercise === &lt;br /&gt;
We will let this part of the tutorial come to a close with the obligatory exercise for the reader: &lt;br /&gt;
In order to have the SimpleHTML block work &amp;quot;correctly&amp;quot;, find out how to strengthen the eager approach to strip out all tags from the existing configuration of all instances of our block, &#039;&#039;&#039;or&#039;&#039;&#039; go back and implement the lazy approach instead. &lt;br /&gt;
(Hint: Do that in the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method.)&lt;br /&gt;
&lt;br /&gt;
=== UPDATING: === &lt;br /&gt;
Prior to version 1.5, the file &#039;&#039;config_global.html&#039;&#039; was named simply &#039;&#039;config.html&#039;&#039;. Also, the methods [[Blocks_Howto#method_config_save| config_save]] and [[Blocks_Howto#method_config_print| config_print]] were named &#039;&#039;&#039;handle_config&#039;&#039;&#039; and &#039;&#039;&#039;print_config&#039;&#039;&#039; respectively. Upgrading a block to work with Moodle 1.5 involves updating these aspects; refer to [[Blocks_Howto#appendix_b| Appendix B]] for more information.&lt;br /&gt;
&lt;br /&gt;
== Eye Candy ==&lt;br /&gt;
&lt;br /&gt;
Our block is just about complete functionally, so now let&#039;s take a look at some of the tricks we can use to make its behavior customized in a few more useful ways.&lt;br /&gt;
&lt;br /&gt;
First of all, there are a couple of ways we can adjust the visual aspects of our block. For starters, it might be useful to create a block that doesn&#039;t display a header (title) at all. You can see this effect in action in the Course Description block that comes with Moodle. This behavior is achieved by, you guessed it, adding one more method to our block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function hide_header() {&lt;br /&gt;
  return TRUE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One more note here: we cannot just set an empty title inside the block&#039;s [[Development:Blocks/Appendix_A#init.28.29| init()]] method; it&#039;s necessary for each block to have a unique, non-empty title after [[Development:Blocks/Appendix_A#init.28.29| init()]] is called so that Moodle can use those titles to differentiate between all of the installed blocks.&lt;br /&gt;
&lt;br /&gt;
Another adjustment we might want to do is instruct our block to take up a certain amount of width on screen. Moodle handles this as a two-part process: first, it queries each block about its preferred width and takes the maximum number as the desired value. Then, the page that&#039;s being displayed can choose to use this value or, more probably, bring it within some specific range of values if it isn&#039;t already. That means that the width setting is a best-effort settlement; your block can &#039;&#039;request&#039;&#039; a certain width and Moodle will &#039;&#039;try&#039;&#039; to provide it, but there&#039;s no guarantee whatsoever about the end result. As a concrete example, all standard Moodle course formats will deliver any requested width between 180 and 210 pixels, inclusive.&lt;br /&gt;
&lt;br /&gt;
To instruct Moodle about our block&#039;s preferred width, we add one more method to the block class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function preferred_width() {&lt;br /&gt;
  // The preferred value is in pixels&lt;br /&gt;
  return 200;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
This will make our block (and all the other blocks displayed at the same side of the page) a bit wider than standard.&lt;br /&gt;
&lt;br /&gt;
Finally, we can also affect some properties of the actual HTML that will be used to print our block. Each block is fully contained within a &amp;amp;lt;table&amp;amp;gt; element, inside which all the HTML for that block is printed. We can instruct Moodle to add HTML attributes with specific values to that container. This would be done to either a) directly affect the end result (if we say, assign bgcolor=&amp;quot;black&amp;quot;), or b) give us freedom to customize the end result using CSS (this is in fact done by default as we &#039;ll see below).&lt;br /&gt;
&lt;br /&gt;
The default behavior of this feature in our case will assign to our block&#039;s container the class HTML attribute with the value &amp;quot;sideblock block_simplehtml&amp;quot; (the prefix &amp;quot;block_&amp;quot; followed by the name of our block, lowercased). We can then use that class to make CSS selectors in our theme to alter this block&#039;s visual style (for example, &amp;quot;.sideblock.block_simplehtml { border: 1px black solid}&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
To change the default behavior, we will need to define a method which returns an associative array of attribute names and values. For example, the version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function html_attributes() {&lt;br /&gt;
  return array(&lt;br /&gt;
    &#039;class&#039;       =&amp;gt; &#039;sideblock block_&#039;. $this-&amp;gt;name(),&lt;br /&gt;
    &#039;onmouseover&#039; =&amp;gt; &amp;quot;alert(&#039;Mouseover on our block!&#039;);&amp;quot;&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
will result in a mouseover event being added to our block using JavaScript, just as if we had written the onmouseover=&amp;quot;alert(...)&amp;quot; part ourselves in HTML. Note that we actually duplicate the part which sets the class attribute (we want to keep that, and since we override the default behavior it&#039;s our responsibility to emulate it if required). &lt;br /&gt;
&lt;br /&gt;
And the final elegant touch is that we don&#039;t set the class to the hard-coded value &amp;quot;block_simplehtml&amp;quot; but instead use the [[Development:Blocks/Appendix_A#name.28.29| name()]] method to make it dynamically match our block&#039;s name.&lt;br /&gt;
&lt;br /&gt;
== Authorized Personnel Only ==&lt;br /&gt;
&lt;br /&gt;
It&#039;s not difficult to imagine a block which is very useful in some circumstances but it simply cannot be made meaningful in others. An example of this would be the &amp;quot;Social Activities&amp;quot; block which is indeed useful in a course with the social format, but doesn&#039;t do anything useful in a course with the weeks format. There should be some way of allowing the use of such blocks only where they are indeed meaningful, and not letting them confuse users if they are not.&lt;br /&gt;
&lt;br /&gt;
Moodle allows us to declare which course formats each block is allowed to be displayed in, and enforces these restrictions as set by the block developers at all times. The information is given to Moodle as a standard associative array, with each key corresponding to a page format and defining a boolean value (true/false) that declares whether the block should be allowed to appear in that page format.&lt;br /&gt;
&lt;br /&gt;
Notice the deliberate use of the term &#039;&#039;page&#039;&#039; instead of &#039;&#039;course&#039;&#039; in the above paragraph. This is because in Moodle 1.5 and onwards, blocks can be displayed in any page that supports them. The best example of such pages are the course pages, but we are not restricted to them. For instance, the quiz view page (the first one we see when we click on the name of the quiz) also supports blocks.&lt;br /&gt;
&lt;br /&gt;
The format names we can use for the pages derive from the name of the script which is actually used to display that page. For example, when we are looking at a course, the script is &amp;lt;span class=&amp;quot;filename&amp;quot;&amp;gt;/course/view.php&amp;lt;/span&amp;gt; (this is evident from the browser&#039;s address line). Thus, the format name of that page is &#039;&#039;&#039;course-view&#039;&#039;&#039;. It follows easily that the format name for a quiz view page is &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039;. This rule of thumb does have a few exceptions, however:&lt;br /&gt;
&lt;br /&gt;
# The format name for the front page of Moodle is &#039;&#039;&#039;site-index&#039;&#039;&#039;.&lt;br /&gt;
# The format name for courses is actually not just &#039;&#039;&#039;course-view&#039;&#039;&#039;&amp;lt;nowiki&amp;gt;; it is &amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;course-view-weeks&#039;&#039;&#039;, &#039;&#039;&#039;course-view-topics&#039;&#039;&#039;, etc.&lt;br /&gt;
# Even though there is no such page, the format name &#039;&#039;&#039;all&#039;&#039;&#039; can be used as a catch-all option.&lt;br /&gt;
&lt;br /&gt;
We can include as many format names as we want in our definition of the applicable formats. Each format can be allowed or disallowed, and there are also three more rules that help resolve the question &amp;quot;is this block allowed into this page or not?&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
# Prefixes of a format name will match that format name; for example, &#039;&#039;&#039;mod&#039;&#039;&#039; will match all the activity modules. &#039;&#039;&#039;course-view&#039;&#039;&#039; will match any course, regardless of the course format. And finally, &#039;&#039;&#039;site&#039;&#039;&#039; will also match the front page (remember that its full format name is &#039;&#039;&#039;site-index&#039;&#039;&#039;).&lt;br /&gt;
# The more specialized a format name that matches our page is, the higher precedence it has when deciding if the block will be allowed. For example, &#039;&#039;&#039;mod&#039;&#039;&#039;, &#039;&#039;&#039;mod-quiz&#039;&#039;&#039; and &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; all match the quiz view page. But if all three are present, &#039;&#039;&#039;mod-quiz-view&#039;&#039;&#039; will take precedence over the other two because it is a better match.&lt;br /&gt;
# The character &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; can be used in place of any word. For example, &#039;&#039;&#039;mod&#039;&#039;&#039; and &#039;&#039;&#039;mod-*&#039;&#039;&#039; are equivalent. At the time of this document&#039;s writing, there is no actual reason to utilize this &amp;quot;wildcard matching&amp;quot; feature, but it exists for future usage.&lt;br /&gt;
# The order that the format names appear does not make any difference.&lt;br /&gt;
All of the above are enough to make the situation sound complex, so let&#039;s look at some specific examples. First of all, to have our block appear &#039;&#039;&#039;only&#039;&#039;&#039; in the site front page, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&#039;site&#039; =&amp;gt; TRUE);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Since &#039;&#039;&#039;all&#039;&#039;&#039; is missing, the block is disallowed from appearing in &#039;&#039;any&#039;&#039; course format; but then &#039;&#039;&#039;site&#039;&#039;&#039; is set to TRUE, so it&#039;s explicitly allowed to appear in the site front page (remember that &#039;&#039;&#039;site&#039;&#039;&#039; matches &#039;&#039;&#039;site-index&#039;&#039;&#039; because it&#039;s a prefix).&lt;br /&gt;
&lt;br /&gt;
For another example, if we wanted to allow the block to appear in all course formats &#039;&#039;except&#039;&#039; social, and also to &#039;&#039;not&#039;&#039; be allowed anywhere but in courses, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;course-view&#039; =&amp;gt; TRUE, &lt;br /&gt;
    &#039;course-view-social&#039; =&amp;gt; FALSE);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This time, we first allow the block to appear in all courses and then we explicitly disallow the social format.&lt;br /&gt;
For our final, most complicated example, suppose that a block can be displayed in the site front page, in courses (but not social courses) and also when we are viewing any activity module, &#039;&#039;except&#039;&#039; quiz. This would be:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
function applicable_formats() {&lt;br /&gt;
  return array(&lt;br /&gt;
           &#039;site-index&#039; =&amp;gt; TRUE,&lt;br /&gt;
          &#039;course-view&#039; =&amp;gt; TRUE, &lt;br /&gt;
   &#039;course-view-social&#039; =&amp;gt; FALSE,&lt;br /&gt;
                  &#039;mod&#039; =&amp;gt; TRUE, &lt;br /&gt;
             &#039;mod-quiz&#039; =&amp;gt; FALSE&lt;br /&gt;
  );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is not difficult to realize that the above accomplishes the objective if we remember that there is a &amp;quot;best match&amp;quot; policy to determine the end result.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UPDATING:&#039;&#039;&#039; &amp;lt;br /&amp;gt;&lt;br /&gt;
Prior to version 1.5, blocks were only allowed in courses (and in Moodle 1.4, in the site front page). Also, the keywords used to describe the valid course formats at the time were slightly different and had to be changed in order to allow for a more open architecture. Refer to [[Development:Blocks/Appendix_B| Appendix B]] for more information on the changes that old blocks have to make to conform to the new standard.&lt;br /&gt;
&lt;br /&gt;
== Lists and Icons ==&lt;br /&gt;
&lt;br /&gt;
In this final part of the guide we will briefly discuss an additional capability of Moodle&#039;s block system, namely the ability to very easily create blocks that display a list of choices to the user. This list is displayed with one item per line, and an optional image (icon) next to the item. An example of such a &#039;&#039;list block&#039;&#039; is the standard Moodle &amp;quot;admin&amp;quot; block, which illustrates all the points discussed in this section.&lt;br /&gt;
&lt;br /&gt;
As we have seen so far, blocks use two properties of [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]]: &amp;quot;text&amp;quot; and &amp;quot;footer&amp;quot;. The text is displayed as-is as the block content, and the footer is displayed below the content in a smaller font size. List blocks use $this-&amp;gt;content-&amp;gt;footer in the exact same way, but they ignore $this-&amp;gt;content-&amp;gt;text.&lt;br /&gt;
&lt;br /&gt;
Instead, Moodle expects such blocks to set two other properties when the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method is called: $this-&amp;gt;content-&amp;gt;items and $this-&amp;gt;content-&amp;gt;icons. $this-&amp;gt;content-&amp;gt;items should be a numerically indexed array containing elements that represent the HTML for each item in the list that is going to be displayed. Usually these items will be HTML anchor tags which provide links to some page. $this-&amp;gt;content-&amp;gt;icons should also be a numerically indexed array, with exactly as many items as $this-&amp;gt;content-&amp;gt;items has. Each of these items should be a fully qualified HTML &amp;lt;img&amp;gt; tag, with &amp;quot;src&amp;quot;, &amp;quot;height&amp;quot;, &amp;quot;width&amp;quot; and &amp;quot;alt&amp;quot; attributes. Obviously, it makes sense to keep the images small and of a uniform size.&lt;br /&gt;
&lt;br /&gt;
In order to tell Moodle that we want to have a list block instead of the standard text block, we need to make a small change to our block class declaration. Instead of extending class &#039;&#039;&#039;block_base&#039;&#039;&#039;, our block will extend class &#039;&#039;&#039;block_list&#039;&#039;&#039;. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
 class block_my_menu extends block_list {&lt;br /&gt;
     // The init() method does not need to change at all&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to making this change, we must of course also modify the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method to construct the [[Development:Blocks/Appendix_A#.24this-.3Econtent| $this-&amp;gt;content]] variable as discussed above:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt; &lt;br /&gt;
function get_content() {&lt;br /&gt;
  if ($this-&amp;gt;content !== NULL) {&lt;br /&gt;
    return $this-&amp;gt;content;&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content         = new stdClass;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons  = array();&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;footer = &#039;Footer here...&#039;;&lt;br /&gt;
 &lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;items[] = &#039;&amp;lt;a href=&amp;quot;some_file.php&amp;quot;&amp;gt;Menu Option 1&amp;lt;/a&amp;gt;&#039;;&lt;br /&gt;
  $this-&amp;gt;content-&amp;gt;icons[] = &#039;&amp;lt;img src=&amp;quot;images/icons/1.gif&amp;quot; class=&amp;quot;icon&amp;quot; alt=&amp;quot;&amp;quot; /&amp;gt;&#039;;&lt;br /&gt;
 &lt;br /&gt;
  // Add more list items here&lt;br /&gt;
 &lt;br /&gt;
  return $this-&amp;gt;content;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To summarize, if we want to create a list block instead of a text block, we just need to change the block class declaration and the [[Development:Blocks/Appendix_A#get_content.28.29| get_content()]] method. Adding the mandatory [[Development:Blocks/Appendix_A#init.28.29| init()]] method as discussed earlier will then give us our first list block in no time!&lt;br /&gt;
&lt;br /&gt;
[[#top|Back to top of page]]&lt;br /&gt;
&lt;br /&gt;
== Appendices ==&lt;br /&gt;
&lt;br /&gt;
The appendices have been moved to separate pages:&lt;br /&gt;
&lt;br /&gt;
* Appendix A: [[Development:Blocks/Appendix A|&#039;&#039;block_base&#039;&#039; Reference]] &lt;br /&gt;
* Appendix B: [[Development:Blocks/Appendix B|Differences in the Blocks API for Moodle Versions prior to 1.5]]&lt;br /&gt;
* Appendix C: [[Development:Blocks/Appendix C|Creating Database Tables for Blocks (prior to 1.7)]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer|Blocks]]&lt;br /&gt;
[[Category:Tutorial]]&lt;br /&gt;
&lt;br /&gt;
[[es:Desarrollo de bloques]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54689</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54689"/>
		<updated>2009-04-22T11:29:23Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above) (http://grolmsnet.de/kerbtut/)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain Controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Linux/UNIX web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Active Directory user account for web server service principal:&#039;&#039;&#039; moodlekerb&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.example.ac.uk and enter the following in krb5.conf (by default: /etc/krb5.conf)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Test kerberos&lt;br /&gt;
Issue the following command at the shell prompt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$&amp;gt; kinit user@EXAMPLE.AC.UK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;user&#039; is an Active Directory account for which you know the password.&lt;br /&gt;
&lt;br /&gt;
Next, issue the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;$&amp;gt;klist&amp;lt;/pre&amp;gt;&lt;br /&gt;
If all is OK it will list the Kerberos ticket you were granted from the domain controller (KDC)&lt;br /&gt;
&lt;br /&gt;
* Create HTTP service principal for moodle.example.ac.uk&lt;br /&gt;
#Create the &#039;moodlekerb&#039; &#039;&#039;&#039;user&#039;&#039;&#039; account in Active Directory (NOT a machine account) to map to the web server service principal (HTTP/moodle.example.ac.uk@EXAMPLE.AC.UK)&lt;br /&gt;
NOTE: moodle.example.ac.uk MUST be the canonical DNS name of the server i.e. an A record (NOT a CNAME). Additionally a valid PTR (reverse DNS) record must exist and match the corresponding A record.&lt;br /&gt;
&lt;br /&gt;
#Use the ktpass.exe utility to map the service principal and create a keytab file&lt;br /&gt;
Apache requires a keytab file, which is generated with ktpass.exe on the Windows Active Directory Domain Controller.&lt;br /&gt;
Shockingly, this component of Windows Server 2003 SP1 does not function correctly so one must obtain a hot fix: http://support.microsoft.com/kb/919557&lt;br /&gt;
&lt;br /&gt;
Run the following command on the domain controller:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C:\path\to\hotfix\ktpass.exe -princ HTTP/moodle.example.ac.uk@EXAMPLE.AC.UK -mapuser EXAMPLE\moodlekerb -crypto DES-CBC-MD5 +DesOnly +setPass +rndPass -ptype KRB5_NT_PRINCIPAL -out moodle.example.ac.uk.keytab&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy C:\path\to\hotfix\moodle.example.ac.uk.keytab to the moodle web server and remember the location (/etc/httpd/moodle.example.ac.uk.keytab or similar)&lt;br /&gt;
&lt;br /&gt;
* Configure Apache / mod_auth_kerb&lt;br /&gt;
Edit the Apache configuration for the moodle host and add the following directives:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        &amp;lt;Directory /path/to/moodle/docs/auth/ldap/&amp;gt;&lt;br /&gt;
                &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
                        AuthName &amp;quot;Moodle&amp;quot;&lt;br /&gt;
                        AuthType Kerberos&lt;br /&gt;
                        KrbAuthRealms EXAMPLE.AC.UK&lt;br /&gt;
                        KrbServiceName HTTP&lt;br /&gt;
                        Krb5Keytab /etc/httpd/moodle.example.ac.uk.keytab&lt;br /&gt;
                        KrbMethodNegotiate on&lt;br /&gt;
                        KrbMethodK5Passwd on&lt;br /&gt;
                        KrbAuthoritative on&lt;br /&gt;
                        require valid-user&lt;br /&gt;
                &amp;lt;/Files&amp;gt;&lt;br /&gt;
        &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54688</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54688"/>
		<updated>2009-04-22T11:25:57Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above) (http://grolmsnet.de/kerbtut/)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain Controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Linux/UNIX web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Active Directory user account for web server service principal:&#039;&#039;&#039; moodlekerb&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.example.ac.uk and enter the following in krb5.conf (by default: /etc/krb5.conf)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Test kerberos&lt;br /&gt;
Issue the following command at the shell prompt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$&amp;gt; kinit user@EXAMPLE.AC.UK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;user&#039; is an Active Directory account for which you know the password.&lt;br /&gt;
&lt;br /&gt;
Next, issue the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;$&amp;gt;klist&amp;lt;/pre&amp;gt;&lt;br /&gt;
If all is OK it will list the Kerberos ticket you were granted from the domain controller (KDC)&lt;br /&gt;
&lt;br /&gt;
* Create HTTP service principal for moodle.example.ac.uk&lt;br /&gt;
#Create the &#039;moodlekerb&#039; &#039;&#039;&#039;user&#039;&#039;&#039; account in Active Directory (NOT a machine account) to map to the web server service principal (HTTP/moodle.example.ac.uk@EXAMPLE.AC.UK)&lt;br /&gt;
NOTE: moodle.example.ac.uk MUST be the canonical DNS name of the server i.e. an A record (NOT a CNAME). Additionally a valid PTR (reverse DNS) record must exist and match the corresponding A record.&lt;br /&gt;
&lt;br /&gt;
#Use the ktpass.exe utility to map the service principal and create a keytab file&lt;br /&gt;
Apache requires a keytab file, which is generated with ktpass.exe on the Windows Active Directory Domain Controller.&lt;br /&gt;
Shockingly, this component of Windows Server 2003 SP1 does not function correctly so one must obtain a hot fix: http://support.microsoft.com/kb/919557&lt;br /&gt;
&lt;br /&gt;
Run the following command on the domain controller:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C:\path\to\hotfix\ktpass.exe -princ HTTP/moodle.example.ac.uk@EXAMPLE.AC.UK -mapuser EXAMPLE\moodlekerb -crypto DES-CBC-MD5 +DesOnly +setPass +rndPass -ptype KRB5_NT_PRINCIPAL -out moodle.example.ac.uk.keytab&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy C:\path\to\hotfix\moodle.example.ac.uk.keytab to the moodle web server and remember the location (/etc/httpd/moodle.example.ac.uk.keytab or similar)&lt;br /&gt;
&lt;br /&gt;
* Configure Apache / mod_auth_kerb&lt;br /&gt;
Edit the Apache configuration for the moodle host and add the following directives:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        &amp;lt;Directory /path/to/moodle/docs/auth/ldap/&amp;gt;&lt;br /&gt;
                &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
                        AuthName &amp;quot;Moodle&amp;quot;&lt;br /&gt;
                        AuthType Kerberos&lt;br /&gt;
                        KrbAuthRealms EXAMPLE.AC.UK&lt;br /&gt;
                        KrbServiceName HTTP&lt;br /&gt;
                        Krb5Keytab /etc/httpd/moodle.example.ac.uk.keytab&lt;br /&gt;
                        KrbMethodNegotiate on&lt;br /&gt;
                        KrbMethodK5Passwd on&lt;br /&gt;
                        KrbAuthoritative on&lt;br /&gt;
                        require valid-user&lt;br /&gt;
                &amp;lt;/Files&amp;gt;&lt;br /&gt;
        &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54686</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54686"/>
		<updated>2009-04-22T10:45:11Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above) (http://grolmsnet.de/kerbtut/)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain Controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Linux/UNIX web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Active Directory user account for web server service principal:&#039;&#039;&#039; moodlekerb&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.example.ac.uk and enter the following in krb5.conf (by default: /etc/krb5.conf)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Test kerberos&lt;br /&gt;
Issue the following command at the shell prompt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$&amp;gt; kinit user@EXAMPLE.AC.UK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;user&#039; is an Active Directory account for which you know the password.&lt;br /&gt;
&lt;br /&gt;
Next, issue the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;$&amp;gt;klist&amp;lt;/pre&amp;gt;&lt;br /&gt;
If all is OK it will list the Kerberos ticket you were granted from the domain controller (KDC)&lt;br /&gt;
&lt;br /&gt;
* Create HTTP service principal for moodle.example.ac.uk&lt;br /&gt;
#Create the &#039;moodlekerb&#039; &#039;&#039;&#039;user&#039;&#039;&#039; account in Active Directory (NOT a machine account) to map to the web server service principal (HTTP/moodle.example.ac.uk@EXAMPLE.AC.UK)&lt;br /&gt;
NOTE: moodle.example.ac.uk MUST be the canonical DNS name of the server i.e. an A record (NOT a CNAME). Additionally a valid PTR (reverse DNS) record must exist and match the corresponding A record.&lt;br /&gt;
&lt;br /&gt;
#Use the ktpass.exe utility to map the service principal and create a keytab file&lt;br /&gt;
Apache requires a keytab file, which is generated with ktpass.exe on the Windows Active Directory Domain Controller.&lt;br /&gt;
Shockingly, this component of Windows Server 2003 SP1 does not function correctly so one must obtain a hot fix: http://support.microsoft.com/kb/919557&lt;br /&gt;
&lt;br /&gt;
Run the following command on the domain controller:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C:\path\to\hotfix\ktpass.exe -princ HTTP/moodle.example.ac.uk@EXAMPLE.AC.UK -mapuser EXAMPLE\moodlekerb -crypto DES-CBC-MD5 +DesOnly +setPass +rndPass -ptype KRB5_NT_PRINCIPAL -out moodle.example.ac.uk.keytab&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy C:\path\to\hotfix\moodle.example.ac.uk.keytab to the moodle web server and remember the location (/etc/httpd/moodle.example.ac.uk.keytab or similar)&lt;br /&gt;
&lt;br /&gt;
* Configure Apache / mod_auth_kerb&lt;br /&gt;
Edit the Apache configuration for the moodle host and add the following directives:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        &amp;lt;Directory /path/to/moodle/docs/auth/ldap/&amp;gt;&lt;br /&gt;
                &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
                        AuthName &amp;quot;Moodle&amp;quot;&lt;br /&gt;
                        AuthType Kerberos&lt;br /&gt;
                        KrbAuthRealms EXAMPLE.AC.UK&lt;br /&gt;
                        KrbServiceName HTTP&lt;br /&gt;
                        Krb5Keytab /etc/httpd/moodle.example.ac.uk.keytab&lt;br /&gt;
                        KrbMethodNegotiate on&lt;br /&gt;
                        KrbMethodK5Passwd on&lt;br /&gt;
                        KrbAuthoritative on&lt;br /&gt;
                        require valid-user&lt;br /&gt;
                &amp;lt;/Files&amp;gt;&lt;br /&gt;
        &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54685</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54685"/>
		<updated>2009-04-22T10:37:29Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above) (http://grolmsnet.de/kerbtut/)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain Controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Linux/UNIX web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Active Directory user account for web server service principal:&#039;&#039;&#039; moodlekerb&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.example.ac.uk and enter the following in krb5.conf (by default: /etc/krb5.conf)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Test kerberos&lt;br /&gt;
Issue the following command at the shell prompt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$&amp;gt; kinit user@EXAMPLE.AC.UK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;user&#039; is an Active Directory account for which you know the password.&lt;br /&gt;
&lt;br /&gt;
Next, issue the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;$&amp;gt;klist&amp;lt;/pre&amp;gt;&lt;br /&gt;
If all is OK it will list the Kerberos ticket you were granted from the domain controller (KDC)&lt;br /&gt;
&lt;br /&gt;
* Create HTTP service principal for moodle.example.ac.uk&lt;br /&gt;
#Create the &#039;moodlekerb&#039; &#039;&#039;&#039;user&#039;&#039;&#039; account in Active Directory (NOT a machine account) to map to the web server service principal (HTTP/moodle.example.ac.uk@EXAMPLE.AC.UK)&lt;br /&gt;
NOTE: moodle.example.ac.uk MUST be the canonical DNS name of the server i.e. an A record (NOT a CNAME). Additionally a valid PTR (reverse DNS) record must exist and match the corresponding A record.&lt;br /&gt;
&lt;br /&gt;
#Use the ktpass.exe utility to map the service principal and create a keytab file&lt;br /&gt;
Apache requires a keytab file, which is generated with ktpass.exe on the Windows Active Directory Domain Controller.&lt;br /&gt;
Shockingly, this component of Windows Server 2003 SP1 does not function correctly so one must obtain a hot fix: http://support.microsoft.com/kb/919557&lt;br /&gt;
&lt;br /&gt;
Run the following command on the domain controller:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C:\path\to\hotfix\ktpass.exe -princ HTTP/moodle.example.ac.uk@EXAMPLE.AC.UK -mapuser EXAMPLE\moodlekerb -crypto DES-CBC-MD5 +DesOnly +setPass +rndPass -ptype KRB5_NT_PRINCIPAL -out moodle.example.ac.uk.keytab&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy C:\path\to\hotfix\moodle.example.ac.uk.keytab to the moodle web server and remember the location (/etc/httpd/moodle.example.ac.uk.keytab or similar)&lt;br /&gt;
&lt;br /&gt;
* Configure Apache / mod_auth_kerb&lt;br /&gt;
Edit the Apache configuration for the moodle host and add the following directives:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        &amp;lt;Directory /path/to/moodle/docs/auth/ldap/&amp;gt;&lt;br /&gt;
                &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
                        AuthName &amp;quot;Moodle&amp;quot;&lt;br /&gt;
                        AuthType Kerberos&lt;br /&gt;
                        KrbAuthRealms EXAMPLE.AC.UK&lt;br /&gt;
                        KrbServiceName HTTP&lt;br /&gt;
                        Krb5Keytab /etc/apache2/moodle.my-diploma.co.uk.keytab&lt;br /&gt;
                        KrbMethodNegotiate on&lt;br /&gt;
                        KrbMethodK5Passwd on&lt;br /&gt;
                        KrbAuthoritative on&lt;br /&gt;
                        require valid-user&lt;br /&gt;
                &amp;lt;/Files&amp;gt;&lt;br /&gt;
        &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54684</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54684"/>
		<updated>2009-04-22T10:33:21Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above) (http://grolmsnet.de/kerbtut/)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain Controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Linux/UNIX web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Active Directory user account for web server service principal:&#039;&#039;&#039; moodlekerb&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.example.ac.uk and enter the following in krb5.conf (by default: /etc/krb5.conf)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Test kerberos&lt;br /&gt;
Issue the following command at the shell prompt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$&amp;gt; kinit user@EXAMPLE.AC.UK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;user&#039; is an Active Directory account for which you know the password.&lt;br /&gt;
&lt;br /&gt;
Next, issue the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;$&amp;gt;klist&amp;lt;/pre&amp;gt;&lt;br /&gt;
If all is OK it will list the Kerberos ticket you were granted from the domain controller (KDC)&lt;br /&gt;
&lt;br /&gt;
* Create HTTP service principal for moodle.example.ac.uk&lt;br /&gt;
#Create the &#039;moodlekerb&#039; &#039;&#039;&#039;user&#039;&#039;&#039; account in Active Directory (NOT a machine account) to map to the web server service principal (HTTP/moodle.example.ac.uk@EXAMPLE.AC.UK)&lt;br /&gt;
NOTE: moodle.example.ac.uk MUST be the canonical DNS name of the server i.e. an A record (NOT a CNAME). Additionally a valid PTR (reverse DNS) record must exist and match the corresponding A record.&lt;br /&gt;
&lt;br /&gt;
#Use the ktpass.exe utility to map the service principal and create a keytab file&lt;br /&gt;
Apache requires a keytab file, which is generated with ktpass.exe on the Windows Active Directory Domain Controller.&lt;br /&gt;
Shockingly, this component of Windows Server 2003 SP1 does not function correctly so one must obtain a hot fix: http://support.microsoft.com/kb/919557&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C:\path\to\hotfix\ktpass.exe -princ HTTP/moodle.example.ac.uk@EXAMPLE.AC.UK -mapuser EXAMPLE\moodlekerb -crypto DES-CBC-MD5 +DesOnly +setPass +rndPass -ptype KRB5_NT_PRINCIPAL -out moodle.example.ac.uk.keytab&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Configure Apache / mod_auth_kerb&lt;br /&gt;
....&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54683</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54683"/>
		<updated>2009-04-22T10:31:07Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above) (http://grolmsnet.de/kerbtut/)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain Controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Linux/UNIX web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Active Directory user account for web server service principal:&#039;&#039;&#039; moodlekerb&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.example.ac.uk and enter the following in krb5.conf (by default: /etc/krb5.conf)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Test kerberos&lt;br /&gt;
Issue the following command at the shell prompt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$&amp;gt; kinit user@EXAMPLE.AC.UK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;user&#039; is an Active Directory account for which you know the password.&lt;br /&gt;
&lt;br /&gt;
Next, issue the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;$&amp;gt;klist&amp;lt;/pre&amp;gt;&lt;br /&gt;
If all is OK it will list the Kerberos ticket you were granted from the domain controller (KDC)&lt;br /&gt;
&lt;br /&gt;
* Create HTTP service principal for moodle.example.ac.uk&lt;br /&gt;
#Create the &#039;moodlekerb&#039; &#039;&#039;&#039;user&#039;&#039;&#039; account in Active Directory (NOT a machine account) to map to the web server service principal (HTTP/moodle.example.ac.uk@EXAMPLE.AC.UK)&lt;br /&gt;
NOTE: moodle.example.ac.uk MUST be the canonical DNS name of the server i.e. an A record (NOT a CNAME). Additionally a valid PTR (reverse DNS) record must exist and match the corresponding A record.&lt;br /&gt;
&lt;br /&gt;
#Use the ktpass.exe utility to map the service principal and create a keytab file&lt;br /&gt;
Apache requires a keytab file, which is generated with ktpass.exe on the Windows Active Directory Domain Controller.&lt;br /&gt;
Shockingly, this component of Windows Server 2003 SP1 does not function correctly so one must obtain a hot fix: http://support.microsoft.com/kb/919557&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Configure Apache / mod_auth_kerb&lt;br /&gt;
....&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54682</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54682"/>
		<updated>2009-04-22T10:28:28Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above) (http://grolmsnet.de/kerbtut/)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain Controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Linux/UNIX web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.example.ac.uk and enter the following in krb5.conf (by default: /etc/krb5.conf)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Test kerberos&lt;br /&gt;
Issue the following command at the shell prompt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$&amp;gt; kinit user@EXAMPLE.AC.UK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;user&#039; is an Active Directory account for which you know the password.&lt;br /&gt;
&lt;br /&gt;
Next, issue the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;$&amp;gt;klist&amp;lt;/pre&amp;gt;&lt;br /&gt;
If all is OK it will list the Kerberos ticket you were granted from the domain controller (KDC)&lt;br /&gt;
&lt;br /&gt;
* Create HTTP service principal for moodle.example.ac.uk&lt;br /&gt;
#Create a &#039;&#039;&#039;user&#039;&#039;&#039; account in Active Directory (NOT a machine account) to map to the web server service principal (HTTP/moodle.example.ac.uk@EXAMPLE.AC.UK)&lt;br /&gt;
NOTE: moodle.example.ac.uk MUST be the canonical DNS name of the server i.e. an A record (NOT a CNAME). Additionally a valid PTR (reverse DNS) record must exist and match the corresponding A record.&lt;br /&gt;
&lt;br /&gt;
#Use the ktpass.exe utility to map the service principal and create a keytab file&lt;br /&gt;
Apache requires a keytab file, which is generated with ktpass.exe on the Windows Active Directory Domain Controller.&lt;br /&gt;
Shockingly, this component of Windows Server 2003 SP1 does not function correctly so one must obtain a hot fix: http://support.microsoft.com/kb/919557&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Configure Apache / mod_auth_kerb&lt;br /&gt;
....&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54680</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54680"/>
		<updated>2009-04-22T10:14:52Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above) (http://grolmsnet.de/kerbtut/)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain Controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Linux/UNIX web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.example.ac.uk and enter the following in krb5.conf (by default: /etc/krb5.conf)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Test kerberos&lt;br /&gt;
Issue the following command at the shell prompt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$&amp;gt; kinit user@EXAMPLE.AC.UK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;user&#039; is an Active Directory account for which you know the password.&lt;br /&gt;
&lt;br /&gt;
Next, issue the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;$&amp;gt;klist&amp;lt;/pre&amp;gt;&lt;br /&gt;
If all is OK it will list the Kerberos ticket you were granted from the domain controller (KDC)&lt;br /&gt;
&lt;br /&gt;
* Create HTTP service principal for moodle.example.ac.uk&lt;br /&gt;
#Create a &#039;&#039;&#039;user&#039;&#039;&#039; account&lt;br /&gt;
&lt;br /&gt;
* Configure Apache / mod_auth_kerb&lt;br /&gt;
....&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54678</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54678"/>
		<updated>2009-04-22T10:11:19Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above) (http://grolmsnet.de/kerbtut/)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain Controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Linux/UNIX web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.example.ac.uk and enter the following in krb5.conf (by default: /etc/krb5.conf)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Test kerberos&lt;br /&gt;
Issue the following command at the shell prompt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$&amp;gt; kinit user@EXAMPLE.AC.UK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;user&#039; is an Active Directory account for which you know the password.&lt;br /&gt;
&lt;br /&gt;
Next, issue the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;$&amp;gt;klist&amp;lt;/pre&amp;gt;&lt;br /&gt;
If all is OK it will list the Kerberos ticket you were granted from the domain controller (KDC)&lt;br /&gt;
&lt;br /&gt;
* Create HTTP service principal for moodle.example.ac.uk&lt;br /&gt;
....&lt;br /&gt;
* Configure Apache / mod_auth_kerb&lt;br /&gt;
....&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54675</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54675"/>
		<updated>2009-04-22T10:09:48Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above) (http://grolmsnet.de/kerbtut/)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain Controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Linux/UNIX web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.example.ac.uk and enter the following in krb5.conf (by default: /etc/krb5.conf)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Test kerberos&lt;br /&gt;
Issue the following command at the shell prompt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$&amp;gt; kinit user@EXAMPLE.AC.UK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;user&#039; is an Active Directory account for which you know the password.&lt;br /&gt;
&lt;br /&gt;
Next, issue the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;$&amp;gt;klist&amp;lt;/pre&amp;gt;&lt;br /&gt;
If all is OK it will list the Kerberos ticket you were granted from the domain controller (KDC)&lt;br /&gt;
&lt;br /&gt;
* Create HTTP service principal for moodle.example.ac.uk&lt;br /&gt;
....&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54671</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54671"/>
		<updated>2009-04-22T10:07:26Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain Controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Linux/UNIX web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.example.ac.uk and enter the following in krb5.conf (by default: /etc/krb5.conf)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Test kerberos&lt;br /&gt;
Issue the following command at the shell prompt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$&amp;gt; kinit user@EXAMPLE.AC.UK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;user&#039; is an Active Directory account for which you know the password.&lt;br /&gt;
&lt;br /&gt;
Next, issue the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;$&amp;gt;klist&amp;lt;/pre&amp;gt;&lt;br /&gt;
If all is OK it will list the Kerberos ticket you were granted from the domain controller (KDC)&lt;br /&gt;
&lt;br /&gt;
* Create HTTP service principal for moodle.example.ac.uk&lt;br /&gt;
....&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54670</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54670"/>
		<updated>2009-04-22T10:06:17Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache on Linux/UNIX (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Active Directory Domain Controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Linux/UNIX web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.example.ac.uk and enter the following in krb5.conf (by default: /etc/krb5.conf)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 *Test kerberos&lt;br /&gt;
Issue the following command at the shell prompt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$&amp;gt; kinit user@EXAMPLE.AC.UK&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where &#039;user&#039; is an Active Directory account for which you know the password.&lt;br /&gt;
&lt;br /&gt;
Next, issue the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;$&amp;gt;klist&amp;lt;/pre&amp;gt;&lt;br /&gt;
If all is OK it will list the Kerberos ticket you were granted from the domain controller (KDC)&lt;br /&gt;
&lt;br /&gt;
* Create HTTP service principal for moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54669</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54669"/>
		<updated>2009-04-22T10:00:33Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Domain controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Moodle web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.ac.uk and enter the following in /etc/krb5.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Replace the &#039;&#039;&#039;ntlmsso_magic&#039;&#039;&#039; function in &#039;&#039;&#039;/auth/ldap/auth.php&#039;&#039;&#039; (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54668</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54668"/>
		<updated>2009-04-22T09:58:51Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
#&#039;&#039;&#039;Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
#&#039;&#039;&#039;Domain controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
#&#039;&#039;&#039;Moodle web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.ac.uk and enter the following in /etc/krb5.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Replace the ntlmsso_magic function in /auth/ldap/auth.php (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
        //  $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54667</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54667"/>
		<updated>2009-04-22T09:55:38Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
&#039;&#039;&#039;Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
&#039;&#039;&#039;Domain controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
&#039;&#039;&#039;Moodle web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.ac.uk and enter the following in /etc/krb5.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Replace the ntlmsso_magic function in /auth/ldap/auth.php (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
        //  $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54666</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54666"/>
		<updated>2009-04-22T09:52:50Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
&#039;&#039;&#039;Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
&#039;&#039;&#039;Domain controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
&#039;&#039;&#039;Moodle web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.ac.uk and enter the following in /etc/krb5.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uk&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Replace the ntlmsso_magic function in /auth/ldap/auth.php (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
        //  $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54665</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54665"/>
		<updated>2009-04-22T09:51:15Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
&#039;&#039;&#039;Domain:&#039;&#039;&#039; EXAMPLE.AC.UK&lt;br /&gt;
&#039;&#039;&#039;Domain controller:&#039;&#039;&#039; dc.example.ac.uk&lt;br /&gt;
&#039;&#039;&#039;Moodle web server:&#039;&#039;&#039; moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.ac.uk and enter the following in /etc/krb5.conf&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uke&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Replace the ntlmsso_magic function in /auth/ldap/auth.php (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
        //  $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54664</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54664"/>
		<updated>2009-04-22T09:42:06Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above)&lt;br /&gt;
&lt;br /&gt;
Environment details in this example:&lt;br /&gt;
Domain: EXAMPLE.AC.UK&lt;br /&gt;
Domain controller: dc.example.ac.uk&lt;br /&gt;
Moodle web server: moodle.example.ac.uk&lt;br /&gt;
&lt;br /&gt;
Install kerberos on moodle.ac.uk and enter the following in /etc/krb5.conf&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
[libdefaults]&lt;br /&gt;
    default_realm = EXAMPLE.AC.UK&lt;br /&gt;
&lt;br /&gt;
[domain_realm]&lt;br /&gt;
    example.ac.uk = EXAMPLE.AC.UK&lt;br /&gt;
[realms]&lt;br /&gt;
     EXAMPLE.AC.UK = {&lt;br /&gt;
                      admin_server = dc.example.ac.uk&lt;br /&gt;
                      kdc          = dc.example.ac.uke&lt;br /&gt;
                    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Replace the ntlmsso_magic function in /auth/ldap/auth.php (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
        //  $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54662</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54662"/>
		<updated>2009-04-22T09:31:10Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
*Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above)&lt;br /&gt;
:#...&lt;br /&gt;
:#...&lt;br /&gt;
*Replace the ntlmsso_magic function in /auth/ldap/auth.php (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
        //  $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54661</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54661"/>
		<updated>2009-04-22T09:29:02Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
=====Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above)=====&lt;br /&gt;
&lt;br /&gt;
:*Replace the ntlmsso_magic function in /auth/ldap/auth.php (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
        //  $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54660</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54660"/>
		<updated>2009-04-22T09:18:36Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache (mod_auth_kerb)====&lt;br /&gt;
*Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
=====Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above)=====&lt;br /&gt;
&lt;br /&gt;
*Replace the ntlmsso_magic function in /auth/ldap/auth.php (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
        //  $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54659</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54659"/>
		<updated>2009-04-22T09:13:38Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache (mod_auth_kerb)====&lt;br /&gt;
#Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
* Configuration of mod_auth_kerb in a Microsoft Windows Active Directory environment (AD 2003 and above)&lt;br /&gt;
&lt;br /&gt;
#Replace the ntlmsso_magic function in /auth/ldap/auth.php (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
        //  $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54657</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54657"/>
		<updated>2009-04-22T09:01:44Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache (mod_auth_kerb)====&lt;br /&gt;
#Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
#Replace the ntlmsso_magic function in /auth/ldap/auth.php (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
        //  $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code change will account for the fact that Kerberos presents the username to REMOTE_USER in the format user@DOMAIN, rather than NTLM&#039;s DOMAIN\user&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54656</id>
		<title>NTLM authentication</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/2x/ca/index.php?title=NTLM_authentication&amp;diff=54656"/>
		<updated>2009-04-22T09:00:06Z</updated>

		<summary type="html">&lt;p&gt;Afhole: /* Using the Kerberos Auth Module for Apache (mod_auth_kerb) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Moodle 1.9}}This document describes how to set up &#039;&#039;&#039;NTLM/Windows Integrated Authentication&#039;&#039;&#039; in Moodle. &lt;br /&gt;
&lt;br /&gt;
This is integrated into Moodle 1.9 onwards.&lt;br /&gt;
&lt;br /&gt;
For earlier versions, it uses a modified version of LDAP Authentication.&lt;br /&gt;
The NTLM Authentication module is available in the Modules and Plugins database here:&lt;br /&gt;
http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314&lt;br /&gt;
&lt;br /&gt;
Note: When a particular note is specific to earlier versions of the NTLM plugin, this is noted by: &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
Integrated Windows Authentication uses the security features of Windows clients and servers. It does not prompt users for a user name and password. The current Windows user information on the client computer is supplied by the browser through a challenge/response authentication process with the Web server for the Moodle site. &lt;br /&gt;
&lt;br /&gt;
==Assumptions==&lt;br /&gt;
&lt;br /&gt;
#You are running MS [[Active Directory]] for Authentication.&lt;br /&gt;
#The Server hosting your website is a member of the Active Directory Domain that your users are also members of.&lt;br /&gt;
#You are able to define people inside your Network (and authenticated to the Domain) from an IP range of computers.&lt;br /&gt;
#&#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; You have &amp;quot;some&amp;quot; basic knowledge of php and are able to configure the index.php with the range of internal IP addresses.&lt;br /&gt;
#You are familar with or have read the LDAP authentication documentation.&lt;br /&gt;
#The Active Directory domain credentials of your users are returned as &#039;&#039;&#039;DOMAINNAME\username&#039;&#039;&#039; from your authentication service. If you are using the Winbind service from the Samba project, this can be untrue, depending on your Winbind configuration settings.&lt;br /&gt;
&lt;br /&gt;
If you can not modify your settings to satisfy this last assumption, then you will need to remove or comment out the line that reads:&lt;br /&gt;
    $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
and add the relevant lines of code to extract the username part from the domain user credentials and store it in $username.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
::&#039;&#039;&#039;VERY IMPORTANT&#039;&#039;&#039;: In Moodle 1.9 and onwards, NTLM authentication depends on [[LDAP authentication]], and NTLM configuration is specified in the LDAP authentication settings page (Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server). So before trying to configure NTLM, make sure you have LDAP_authentication properly setup and working.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.9==&lt;br /&gt;
&lt;br /&gt;
No installation needed. See the Administration &amp;gt;&amp;gt; Users &amp;gt;&amp;gt; Authentication &amp;gt;&amp;gt; LDAP Server for the NTLM config options. You only have to&lt;br /&gt;
&lt;br /&gt;
*Enable NTLM SSO&lt;br /&gt;
*Set the IP/Subnet mask for the clients (see below)&lt;br /&gt;
*On IIS: turn on Integrated Authentication&lt;br /&gt;
*On Apache - use one of the 3 methods outlined below&lt;br /&gt;
&lt;br /&gt;
If you have used previous versions of NTLM in your Moodle database you will need to make two further changes. &lt;br /&gt;
&lt;br /&gt;
#The type of authentication held against each user now needs to be LDAP, as NTLM will not be recognised. To edit the fields open up a SQL query for your Moodle server and use the following query &amp;quot;update mdl_user set auth = &#039;ldap&#039; where auth = &#039;ntlm&#039; &amp;quot;&lt;br /&gt;
#If you had a previous .htaccess file in the auth/ntlm directory, you will need to move it to the auth/ldap directory. Regardless of whether it is in a .htaccess file of the httpd.conf, the &amp;lt;Files&amp;gt; line now needs to refer to ntlmsso_magic.php. If it is in the httpd.conf, the &amp;lt;Directory&amp;gt; will need to change too. This is covered later on for new installs, but is one of the fundamental changes that needs to be made for those upgrading.&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.6/1.7==&lt;br /&gt;
#Copy the folder AUTH/NTLM into the AUTH folder of your moodle installation.&lt;br /&gt;
#Configure the IP/Subnet Mask in the Config screen.&lt;br /&gt;
[https://docs.moodle.org/en/NTLM_authentication#Configuring IP/Subnet Mask see below for more help]&lt;br /&gt;
If the IP/Subnet Mask does not give enough complexity for your network, Modify the auth/ntlm/index.php file - for instructions on doing this, view the comments in the file.&lt;br /&gt;
#Turn Integrated Authentication ON and Anonymous Authentication OFF for the moodle\auth\ntlm\oncampuslogin.php file. [https://docs.moodle.org/en/NTLM_authentication#How_to_Turn_Integrated_Authentication_on see below for more detailed instructions]&lt;br /&gt;
#Visit the admin page of your moodle installation - you should see notification that the NTLM_AUTH module has been installed.&lt;br /&gt;
#go to the configuration &amp;gt; variables page, find the dbsessions setting (in 1.8 on admin page server \ sessions page), and set it to &amp;quot;YES&amp;quot; then save the page.&lt;br /&gt;
#go to the Authentication admin page and select auth_ntlmtitle as your authentication method Note: - this doesn&#039;t display full text as I haven&#039;t created a language file for this module - you will also see auth_ntlmdescription instead of a proper description - you don&#039;t need to worry about this, as you will be the only one who ever sees this.&lt;br /&gt;
#Configure this page with your normal LDAP settings. NOTE: the Alternate Login URL at the bottom of this page (or on the main authentication page in 1.8 - and needs to be set manually to the oncampus url)has been set to the NTLM page. - if you wish uninstall this auth module, you must reset this variable on the new authentication type page. eg - if you wish to revert back to manual authentication, then change to manual, and then make sure you delete the alternate login url at the bottom of the page.&lt;br /&gt;
#(OPTIONAL) modify the offcampuslogin page to give errors when students try to prefix their usercode with your domain.&lt;br /&gt;
around line 216 find this code, uncomment all the lines and replace the letters &#039;DOM&#039; with your domain:&lt;br /&gt;
&lt;br /&gt;
    if (empty($errormsg)) {&lt;br /&gt;
        if (strstr(strtolower($frm-&amp;gt;username), &amp;quot;DOM\\&amp;quot;) &amp;lt;&amp;gt; false) { //NAD - DOM messages.&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; DOM\\ is not required!&amp;quot;;&lt;br /&gt;
        } else if (strpos($frm-&amp;gt;username, &amp;quot;@&amp;quot;) &amp;lt;&amp;gt; false) {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;) . &amp;quot; enter your username - not your e-mail address.&amp;quot;;&lt;br /&gt;
        } else {&lt;br /&gt;
            $errormsg = get_string(&amp;quot;invalidlogin&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Installation on 1.5==&lt;br /&gt;
&lt;br /&gt;
See the README in the auth/ntml package.&lt;br /&gt;
&lt;br /&gt;
==How to Turn Integrated Authentication on==&lt;br /&gt;
The File ntlmsso_magic.php (1.9 or above) or oncampuslogin.php (1.8 or below) MUST have NTLM/Integrated Authentication enabled at the server or the page will not work.&lt;br /&gt;
===IIS Configuration===&lt;br /&gt;
Open up IIS, and find the auth/ldap/ntlmsso_magic.php (1.9 or above) or auth/ntlm/oncampuslogin.php (1.8 or below) file, &lt;br /&gt;
#right click on the file, choose properties&lt;br /&gt;
#under the &amp;quot;file security&amp;quot; tab, click on the Authentication and Access control &amp;quot;edit&amp;quot; button&lt;br /&gt;
#untick &amp;quot;Enable Anonymous Access&amp;quot; and tick &amp;quot;Integrated Windows Authentication&amp;quot;&lt;br /&gt;
===APACHE Configuration===&lt;br /&gt;
There are currently 3 possible methods for this:&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM part of Samba (Linux)====&lt;br /&gt;
&lt;br /&gt;
* Get the plugin here: http://samba.org/ftp/unpacked/lorikeet/mod_auth_ntlm_winbind/ . You need to download all the files from the link, but not the &amp;lt;code&amp;gt;contrib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;debian&amp;lt;/code&amp;gt; directories. Then follow the instructions given inside the &amp;lt;code&amp;gt;README&amp;lt;/code&amp;gt; file. If you are using Debian/Ubuntu, you can follow these [[#Compiling_mod_auth_ntlm_winbind_on_Debian.2FUbuntu|compilation instructions]].&lt;br /&gt;
* Once you have compiled it, put it inside Apache&#039;s modules subdirectory (this location depends on a number of factors, like compiling Apache yourself, using different Linux distributions packages, an so on), and load and enable the module in Apache&#039;s configuration. For example, if your Apache modules are under &amp;lt;tt&amp;gt;/usr/lib/apache2/modules&amp;lt;/tt&amp;gt;, you&#039;ll need something like this in your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;IfModule !mod_auth_ntlm_winbind.c&amp;gt;&lt;br /&gt;
       LoadModule auth_ntlm_winbind_module /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
   &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Install the Samba &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; daemon package. This packages relies on Samba&#039;s configuration file to get some important settings (like the Windows domain name, uid and gid range mappings, and so on). In addition to that, you&#039;ll need to make your Linux/Unix machine part of the domain. Otherwise winbind won&#039;t be able to pull user and groups informationi from the domain controllers. You should read the Samba documentation to perform this step, but the most important part is having something like the following lines in your &amp;lt;code&amp;gt;smb.conf&amp;lt;/code&amp;gt; file (in addition to what you already have there):&lt;br /&gt;
&lt;br /&gt;
  workgroup = DOMAINNAME&lt;br /&gt;
  password server = *&lt;br /&gt;
  security = domain&lt;br /&gt;
  encrypt passwords = true&lt;br /&gt;
  idmap uid = 10000-20000&lt;br /&gt;
  idmap gid = 10000-20000&lt;br /&gt;
&lt;br /&gt;
: and executing the command (as root):&lt;br /&gt;
&lt;br /&gt;
  # net join DOMAINNAME -U Administrator&lt;br /&gt;
&lt;br /&gt;
: where &#039;&#039;&#039;DOMAINNAME&#039;&#039;&#039; is the NetBIOS windows domain name, and &#039;&#039;&#039;Administrator&#039;&#039;&#039; an account with enough privileges to add new machines to the domain.&amp;lt;br/&amp;gt; You&#039;ll need to type this account&#039;s password for the command to succeed.&lt;br /&gt;
&lt;br /&gt;
: Also, make sure you have disabled &amp;quot;Microsoft Network Server: digitally sign communications (always)&amp;quot; in your Domain Controllers Security Policy, unless you are using a version of Samba that can sign SMB packets.&lt;br /&gt;
&lt;br /&gt;
* Restart the &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; service to apply the changes and test that it&#039;s running ok by executing:&lt;br /&gt;
&lt;br /&gt;
  $ wbinfo -u&lt;br /&gt;
&lt;br /&gt;
: You should get the full list of Windows domain users. If you use &#039;&#039;&#039;&amp;lt;tt&amp;gt;-g&amp;lt;/tt&amp;gt;&#039;&#039;&#039; instead, you&#039;ll get the domain groups list.&lt;br /&gt;
&lt;br /&gt;
* Check that your &amp;lt;tt&amp;gt;winbind&amp;lt;/tt&amp;gt; package installed the authentication helper command &amp;lt;tt&amp;gt;ntlm_auth&amp;lt;/tt&amp;gt;, as we&#039;ll need it later. We&#039;ll assume the helper is located at &amp;lt;tt&amp;gt;/usr/bin/ntlm_auth&amp;lt;/tt&amp;gt;. If yours is at a different location, make sure you adjust the path in the example below.&lt;br /&gt;
&lt;br /&gt;
* Add something like this to your Apache configuration file (usually called &amp;lt;tt&amp;gt;apache2.conf&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;http2.conf&amp;lt;/tt&amp;gt;). We&#039;ll assume that your Moodle &amp;lt;tt&amp;gt;$CFG-&amp;gt;dirroot&amp;lt;/tt&amp;gt; directory is located at &amp;lt;tt&amp;gt;/var/www/moodle&amp;lt;/tt&amp;gt; in the example:&lt;br /&gt;
: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ldap/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;/var/www/moodle/auth/ntlm/&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            NTLMAuth on&lt;br /&gt;
            AuthType NTLM&lt;br /&gt;
            AuthName &amp;quot;Moodle NTLM Authentication&amp;quot;&lt;br /&gt;
            NTLMAuthHelper &amp;quot;/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp&amp;quot;&lt;br /&gt;
            NTLMBasicAuthoritative on&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
* Check the permissions of the Winbind pipe directory (Ubuntu places it under &amp;lt;tt&amp;gt;/var/run/samba/winbindd_privileged&amp;lt;/tt&amp;gt;, yours may be placed at a different location). Apache will need to be able to enter that directory, so we need to make sure it has the right permissions. So have a look at the permissions of that directory and note the name of the group assigned to it. The following example is from a Ubuntu 7.10 machine:&lt;br /&gt;
&lt;br /&gt;
  $ ls -ald /var/run/samba/winbindd_privileged&lt;br /&gt;
  drwxr-x--- 2 root winbindd_priv 60 2007-11-17 16:18 /var/run/samba/winbindd_privileged/&lt;br /&gt;
&lt;br /&gt;
:so we see the group is &amp;lt;tt&amp;gt;winbindd_priv&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* Instead of modifying the directory permissions (which could break other services that use winbind) we are goint to make the Apache user (&amp;lt;tt&amp;gt;www-data&amp;lt;/tt&amp;gt; in our example, but could be &amp;lt;tt&amp;gt;httpd&amp;lt;/tt&amp;gt;, or &amp;lt;tt&amp;gt;nobody&amp;lt;/tt&amp;gt;, etc.) is part of the appropiate group. Execute the following as root:&lt;br /&gt;
&lt;br /&gt;
  # adduser www-data winbindd_priv&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt; is available in Debian and Ubuntu at least. If your distribution doesn&#039;t have &amp;lt;tt&amp;gt;adduser&amp;lt;/tt&amp;gt;, you can edit &amp;lt;tt&amp;gt;/etc/group&amp;lt;/tt&amp;gt; manually to achive the same effect.&lt;br /&gt;
&lt;br /&gt;
* Restart the Apache service to apply the changes. Have a look at Apache&#039;s error log to see that everything is ok.&lt;br /&gt;
&lt;br /&gt;
* Couple of gotchas - in Fedora Core, keep alive is turned OFF by default in the httpd.conf - see this bug for further info: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=188138&amp;lt;br /&amp;gt;&lt;br /&gt;
* Email Dan if you get this working - I&#039;m keen to hear how people go using the samba winbind option!&lt;br /&gt;
::-- Hi Dan! I made it work using Ubuntu 7.04. That&#039;s what I&#039;ve used to update the documentation. [[User:Iñaki Arenaza|Iñaki Arenaza]] 10:43, 30 September 2007 (CDT)&lt;br /&gt;
&lt;br /&gt;
====Using the NTLM Auth Module for Apache====&lt;br /&gt;
#get the Module from: http://modntlm.sourceforge.net/&lt;br /&gt;
#use something like this in your httpd.conf: http://moodle.org/mod/forum/discuss.php?d=45887#211074&lt;br /&gt;
&lt;br /&gt;
====Using the mod_auth_sspi Module for Apache 2 on Windows====&lt;br /&gt;
NOTE: This setup is currently being used in a live production environment, and is therefore suitable for such use provided it is correctly configured and tested.&lt;br /&gt;
&lt;br /&gt;
This is the recommended method for Apache 2 on Windows, however it will &#039;&#039;&#039;not&#039;&#039;&#039; work on Linux/UNIX systems.&lt;br /&gt;
It provides better stability and higher performance than other NTLM modules.&lt;br /&gt;
&lt;br /&gt;
* Download the mod_auth_sspi Module from: http://sourceforge.net/projects/mod-auth-sspi/. At the moment of writing this (2007.09.30), the current version is mod_auth_sspi 1.0.4, which has two different ZIP files to download:&lt;br /&gt;
&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.0.58.zip :   Use this file if you are using Apache 2.0.x.&lt;br /&gt;
::* mod_auth_sspi-1.0.4-2.2.2.zip :   Use this file if you are using Apache 2.2.x.&lt;br /&gt;
&lt;br /&gt;
* Unzip the right file and copy mod_auth_sspi.so (it&#039;s inside &#039;&#039;&#039;bin&#039;&#039;&#039; subdirectory) to your Apache modules directory.&lt;br /&gt;
* Edit your Apache 2 configuration file (httpd.conf) to load the module.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;IfModule !mod_auth_sspi.c&amp;gt;&lt;br /&gt;
        LoadModule sspi_auth_module modules/mod_auth_sspi.so&lt;br /&gt;
    &amp;lt;/IfModule&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Choose one of the two methods below&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 1&#039;&#039;&#039;: This method is recommended for servers that will host a single Moodle instance. Configure NTLM from the main configuration file, add the following to httpd.conf (substitute &amp;quot;C:\moodle&amp;quot; with the path to your Moodle installation e.g. &amp;quot;C:\my-moodle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ldap&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below use&#039;&#039;&#039;:&lt;br /&gt;
    &amp;lt;Directory &amp;quot;C:\moodle\auth\ntlm&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
            AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
            AuthType SSPI&lt;br /&gt;
            SSPIAuth On&lt;br /&gt;
            SSPIOfferBasic Off&lt;br /&gt;
            SSPIAuthoritative On&lt;br /&gt;
            SSPIDomain mycollege.ac.uk&lt;br /&gt;
            require valid-user&lt;br /&gt;
        &amp;lt;/Files&amp;gt;&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: &#039;&#039;&#039;Method 2&#039;&#039;&#039;: The alternative method is to use a .htaccess file&lt;br /&gt;
:This method is recommended for servers that will host multiple Moodle instances. It allows additional Moodle instances to be configured without restarting apache, and also makes the solution a little more portable. We need to add a directive to the main httpd.conf to allow configuration of authentication within .htaccess files.&lt;br /&gt;
    &amp;lt;Directory C:\moodle&amp;gt;&lt;br /&gt;
        AllowOverride AuthConfig&lt;br /&gt;
    &amp;lt;/Directory&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.9 or above&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ldap&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files ntlmsso_magic.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:: &#039;&#039;&#039;For 1.8 or below&#039;&#039;&#039;:&lt;br /&gt;
:::Create a new text file named &#039;.htaccess&#039; in the directory &#039;C:\moodle\moodle\auth\ntlm&#039; and add the following directives:&lt;br /&gt;
    &amp;lt;Files oncampuslogin.php&amp;gt;&lt;br /&gt;
        AuthName &amp;quot;Moodle at My College&amp;quot;&lt;br /&gt;
        AuthType SSPI&lt;br /&gt;
        SSPIAuth On&lt;br /&gt;
        SSPIOfferBasic Off&lt;br /&gt;
        SSPIAuthoritative On&lt;br /&gt;
        SSPIDomain mycollege.ac.uk&lt;br /&gt;
        require valid-user&lt;br /&gt;
    &amp;lt;/Files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:This enables the Moodle folder to be moved to any apache webserver that is configured to allow authentication configuration through .htaccess&lt;br /&gt;
&lt;br /&gt;
For further help and discussion: http://moodle.org/mod/forum/discuss.php?d=56565&lt;br /&gt;
&lt;br /&gt;
====Using the Kerberos Auth Module for Apache (mod_auth_kerb)====&lt;br /&gt;
#Install and configure http://modauthkerb.sourceforge.net/&lt;br /&gt;
#Replace the ntlmsso_magic function in /auth/ldap/auth.php (1.9 and later only?) with the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
    function ntlmsso_magic($sesskey) {&lt;br /&gt;
        if (isset($_SERVER[&#039;REMOTE_USER&#039;]) &amp;amp;&amp;amp; !empty($_SERVER[&#039;REMOTE_USER&#039;])) {&lt;br /&gt;
			&lt;br /&gt;
            $username = $_SERVER[&#039;REMOTE_USER&#039;];&lt;br /&gt;
&lt;br /&gt;
			/**&lt;br /&gt;
			  * begin kerberos - afhole@wortech.ac.uk 21-04-2009&lt;br /&gt;
			  */&lt;br /&gt;
			if ( $pos = strpos($username, &amp;quot;@&amp;quot;) )&lt;br /&gt;
			{&lt;br /&gt;
				$username = substr($username, 0, $pos);&lt;br /&gt;
			} else {&lt;br /&gt;
				$username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
			}&lt;br /&gt;
			/**&lt;br /&gt;
			  * end kerberos&lt;br /&gt;
			  */&lt;br /&gt;
			&lt;br /&gt;
        //  $username = substr(strrchr($username, &#039;\\&#039;), 1); //strip domain info&lt;br /&gt;
            $username = moodle_strtolower($username); //compatibility hack&lt;br /&gt;
            set_cache_flag(&#039;auth/ldap/ntlmsess&#039;, $sesskey, $username, AUTH_NTLMTIMEOUT);&lt;br /&gt;
            return true;&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Configuring IP/Subnet Mask==&lt;br /&gt;
Subnet masks are based on binary patterns so need a bit of knowledge to understand. The best way to find out what IP/Subnet masks to use is to ask your Network Admin. &lt;br /&gt;
* &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; Once you have configured your IP/Subnet masks, you can use the check_ip.php page to test if you have set these ranges up correctly.&lt;br /&gt;
* The new way of specifiying subnets is even easier/more flexible than before 1.9. Just type them one after the other, separated by commas. You can use several syntaxes:&lt;br /&gt;
** Type the network-number/prefix-length combination. E.g. 192.168.1.0/24&lt;br /&gt;
** Type the network &#039;prefix&#039;, ending in a period character. E.g. 192.168.1.&lt;br /&gt;
** Type the network address range (&#039;&#039;&#039;this only works for the last address octect&#039;&#039;&#039;). E.g. 192.168.1.1-254&lt;br /&gt;
:All the three examples refer to the same subnetwork. So assuming you need to specify the following subnetworks:&lt;br /&gt;
::* 10.1.0/255.255.0.0&lt;br /&gt;
::* 10.2.0.0/255.255.0.0&lt;br /&gt;
::* 172.16.0.0/255.255.0.0&lt;br /&gt;
::* 192.168.100.0/255.255.255.240&lt;br /&gt;
:You can type:&lt;br /&gt;
 10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.0/28&lt;br /&gt;
: or:&lt;br /&gt;
  10.1.0.0/16, 10.2.0.0/16, 172.16.0.0/16, 192.168.100.240-255&lt;br /&gt;
:or even:&lt;br /&gt;
  10.1., 10.2., 172.16., 192.168.100.0/28&lt;br /&gt;
:(the last one cannot be expressed as a network &#039;prefix&#039; as the netmask does not fall on an octect boundary).&lt;br /&gt;
&lt;br /&gt;
==Notes/Tips==&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; When using IIS, dbsessions is required to be set to &amp;quot;YES&amp;quot; because when Integrated authentication is turned on for the oncampuslogin.php page, and dbsessions is set to &amp;quot;NO&amp;quot; then the server impersonates the user to write the session in the moodledata\sessions folder. The recommended fix is to set dbsessions to &amp;quot;YES&amp;quot; so that sessions are stored in the db. The non-recommended alternative method is to allow domain users write access to the sessions directory.&lt;br /&gt;
# &#039;&#039;&#039;(pre-1.9 only)&#039;&#039;&#039; If you forget to change the internal IP addresses in index.php to your own, you can just use the offcampuslogin url to login using your admin account. eg: http://yoursite.com/moodle/auth/ntlm/offcampuslogin.php&lt;br /&gt;
#If you are using Firefox, you will need to follow these steps:&lt;br /&gt;
:*Load Firefox and type about:config in the address box. The configuration settings page should be displayed.&lt;br /&gt;
:*In the Filter box, type the word &amp;quot;ntlm&amp;quot; to filter the NTLM strings. You should see three settings displayed.&lt;br /&gt;
:*Double-click on &amp;quot;network.automatic-ntlm-auth.trusted-uris&amp;quot;.&lt;br /&gt;
:*In the box, enter the full URL of your Moodle server. For example &amp;lt;pre&amp;gt;http://moodle.mydomain.com, (the comma is important)&amp;lt;/pre&amp;gt;&lt;br /&gt;
:*Close Firefox and restart.&lt;br /&gt;
&lt;br /&gt;
==Specific File information (pre-1.9 only)== &lt;br /&gt;
(mainly for developers)&lt;br /&gt;
#auth\ntlm\index.php&amp;lt;br /&amp;gt;This is the page used for the Alternate Login URL setting on the config page for the NTLM plugin.&amp;lt;br&amp;gt;The index.php file handles which login page to use based on the IP address of the user.&amp;lt;br&amp;gt;if inside your network, they should be directed to the oncampuslogin.php screen.&amp;lt;br&amp;gt;if outside your network, they should be directed to the offcampuslogin.php screen.&amp;lt;br&amp;gt;you will need to modify the if statements in this file to match the IP ranges inside your network.&lt;br /&gt;
#auth\ntlm\index_form.html&amp;lt;br /&amp;gt;this is a copy of the file login\index_form.php.&amp;lt;br /&amp;gt; The only change in this file from the standard one is that the form action=&amp;quot;index.php&amp;quot; is changed to form action=&amp;quot;offcampuslogin.php&amp;quot; this is because anyone who is displayed the form will be an offcampus user.&lt;br /&gt;
#auth\ntlm\offcampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file moodle\login\index.php with a couple of minor modifications.&amp;lt;br /&amp;gt;the modifications to this file involve the setting of a variable ($onoroffcampus = &amp;quot;offcampus&amp;quot;;) this is used by the auth plugin to define which page is being used for authentication. the other modification is for displaying extra error messages to the user. - with all the authentication methods we have students are constantly confused about how to enter their credentials if you use NTLM authentication elsewhere at your site you will be aware of the users having to enter the domain\username when authenticating. - this code block sits around line 215 in the file.&lt;br /&gt;
#auth\ntlm\oncampuslogin.php&amp;lt;br /&amp;gt;this is a copy of the file login\index.php&amp;lt;br /&amp;gt;This file has been modified to get the details of the authenticated user via NTLM.&lt;br /&gt;
&lt;br /&gt;
==Compiling mod_auth_ntlm_winbind on Debian/Ubuntu==&lt;br /&gt;
You need to install the following packages (and all of their dependencies) by using aptitude, synaptic, etc.:&lt;br /&gt;
&lt;br /&gt;
  autoconf apache2-threaded-dev debian-builder&lt;br /&gt;
&lt;br /&gt;
Once you have them installed, open up a text console, go to the directory where you downloaded the mod_auth_ntlm_winbind files an execute the following commands (as a normal user):&lt;br /&gt;
&lt;br /&gt;
  autoconf&lt;br /&gt;
  ./configure --with-apxs=/usr/bin/apxs2 --with-apache=/usr/sbin/apache2&lt;br /&gt;
  make&lt;br /&gt;
&lt;br /&gt;
That should compile it without errors. Then as a user that can run commands as root via sudo, execute the following command from the same directory:&lt;br /&gt;
&lt;br /&gt;
  sudo make install&lt;br /&gt;
&lt;br /&gt;
This will create the final mod_auth_ntlm_winbind.so file and install it under /usr/lib/apache2/modules, with the rest of the Apache 2 modules (the size of the file and last modification time shown below may differ from your install):&lt;br /&gt;
&lt;br /&gt;
  ls -l /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
  -rw-r--r-- 1 root root 20921 2009-02-17 04:27 /usr/lib/apache2/modules/mod_auth_ntlm_winbind.so&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[http://moodle.org/mod/forum/view.php?id=42 Using Moodle: User authentication] forum&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=45887 NTLM Authentication] forum discussion&lt;br /&gt;
*[http://moodle.org/mod/data/view.php?d=13&amp;amp;rid=314 Download the NTLM Authentication Module]&lt;br /&gt;
*Using Moodle [http://moodle.org/mod/forum/discuss.php?d=80104 Merging AD NTLM SSO into auth/ldap] forum discussion&lt;br /&gt;
&lt;br /&gt;
[[Category:Contributed code]]&lt;br /&gt;
[[Category:Authentication]]&lt;br /&gt;
&lt;br /&gt;
[[fr:Authentification NTLM]]&lt;/div&gt;</summary>
		<author><name>Afhole</name></author>
	</entry>
</feed>