commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / librejs / librejs.module
1 <?php
2
3 /**
4 * Implements hook_js_alter().
5 */
6 function librejs_js_alter(&$javascript) {
7 static $inline, $librejs;
8 // Add inline JavaScript license statement.
9 if (!$inline) {
10 $inline = array(
11 'type' => 'inline',
12 'group' => JS_LIBRARY,
13 'every_page' => TRUE,
14 'weight' => -100,
15 'scope' => 'header',
16 'cache' => TRUE,
17 'defer' => FALSE,
18 'preprocess' => TRUE,
19 'version' => NULL,
20 'data' => theme('librejs_inline_license'),
21 );
22 }
23 $javascript[] = $inline;
24 if (!$librejs) {
25 $librejs = db_query('SELECT data, license, version FROM {librejs}')->fetchAllAssoc('data');
26 }
27 foreach ($javascript as $key => $js) {
28 if ($js['type'] == 'file' || $js['type'] == 'external') {
29 // Aggregated JavaScript will be tagged as GPLv3, so do not aggregate
30 // non-GPLv3-compatible JavaScript.
31 if (empty($librejs[$js['data']]->license)) {
32 $javascript[$key]['preprocess'] = FALSE;
33 }
34 // Merge JavaScript into database if not present or version has changed.
35 if (!isset($librejs[$js['data']]) || (isset($js['version']) && $librejs[$js['data']]->version != $js['version'])) {
36 $key = array('data' => $js['data']);
37 $fields = $key;
38 $fields['type'] = $js['type'];
39 if (isset($js['version'])) {
40 $fields['version'] = $js['version'];
41 }
42 db_merge('librejs')->key($key)->fields($fields)->execute();
43 }
44 }
45 }
46 }
47
48 /**
49 * Returns a list of LibreJS-compatible licenses.
50 */
51 function librejs_licenses() {
52 return array(
53 'http://www.gnu.org/licenses/gpl-2.0.html' => 'GNU-GPL-2.0-or-later',
54 'http://www.gnu.org/licenses/gpl-3.0.html' => 'GNU-GPL-3.0-or-later',
55 'http://www.gnu.org/licenses/lgpl-2.1.html' => 'GNU-LGPL-2.1-or-later',
56 'http://www.gnu.org/licenses/lgpl-3.0.html' => 'GNU-LGPL-3.0-or-later',
57 'http://www.gnu.org/licenses/agpl-3.0.html' => 'GNU-AGPL-3.0-or-later',
58 'http://www.apache.org/licenses/LICENSE-2.0' => 'Apache-2.0-only',
59 'http://directory.fsf.org/wiki/License:BSD_3Clause' => 'Modified-BSD',
60 'http://creativecommons.org/publicdomain/zero/1.0/legalcode' => 'CC0-1.0-only',
61 'http://www.jclark.com/xml/copying.txt' => 'Expat',
62 'http://www.mozilla.org/MPL/2.0' => 'MPL-2.0-or-later',
63 '' => t('Other'),
64 );
65 }
66
67 /**
68 * Implements hook_menu().
69 */
70 function librejs_menu() {
71 $items['librejs/jslicense'] = array(
72 'access arguments' => array('access jslicense'),
73 'page callback' => 'librejs_jslicense',
74 'title' => 'JavaScript license information',
75 'description' => 'License and source code of JavaScript used by the site.',
76 'file' => 'librejs.pages.inc',
77 'options' => array('attributes' => array('rel' => 'jslicense')),
78 );
79 $items['admin/config/development/librejs'] = array(
80 'access arguments' => array('administer site configuration'),
81 'page callback' => 'drupal_get_form',
82 'page arguments' => array('librejs_admin_settings'),
83 'title' => 'LibreJS',
84 'description' => 'Configure the license and source of JavaScript used by the site.',
85 'file' => 'librejs.admin.inc',
86 );
87 return $items;
88 }
89
90 /**
91 * Implements hook_page_build().
92 */
93 function librejs_page_build(&$page) {
94 if (user_access('access jslicense')) {
95 $page['page_bottom']['librejs'] = array(
96 '#type' => 'markup',
97 '#markup' => l(t('JavaScript license information'), 'librejs/jslicense', array('attributes' => array('rel' => 'jslicense', 'style' => 'display: none'))),
98 );
99 }
100 }
101
102 /**
103 * Implements hook_permission().
104 */
105 function librejs_permission() {
106 return array(
107 'access jslicense' => array(
108 'title' => t('Access JavaScript license information'),
109 'description' => t('Roles granted this permission can identify and read all JavaScript files used by the site.'),
110 ),
111 );
112 }
113
114 /**
115 * Implements hook_theme().
116 */
117 function librejs_theme($existing, $type, $theme, $path) {
118 return array(
119 'librejs_inline_license' => array(
120 'template' => 'librejs-inline-license',
121 'variables' => array('year' => NULL, 'author' => NULL),
122 ),
123 );
124 }
125
126 /**
127 * Preprocess variables for librejs-inline-license.tpl.php
128 */
129 function template_preprocess_librejs_inline_license(&$variables) {
130 $variables['year'] = date('Y');
131 $variables['author'] = check_plain(variable_get('site_name', 'Drupal'));
132 }