-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.inc
1409 lines (1268 loc) · 50.2 KB
/
misc.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// $Id: misc.inc 4973 2011-01-26 21:12:56Z dan $
/**
* @file
* Miscellaneous MM functions
*/
define('MM_NODE_INFO_NO_RENDER', 'no_render'); // don't render this node type
define('MM_NODE_INFO_ADD_HIDDEN', 'add_hidden'); // hide the Add node link for this type
define('MM_HOME_MMTID_DEFAULT', 7);
define('MM_MENU_BID', 1); // block ID (bid) containing the page section title
/**
* Get a standard "Access Denied" message
*
* @return
* The message
*/
function mm_access_denied() {
global $user;
drupal_set_header('HTTP/1.0 403 Forbidden');
$path403 = '';
// First, try the original 403 location, saved by the httpauth module
if (module_exists('httpauth')) {
$path403 = drupal_get_normal_path(variable_get('httpauth_site_403', ''));
}
// Failing that, read the Drupal one
if (empty($path403)) {
$path403 = variable_get('site_403', '');
if (empty($path403)) return t('<h2>Password Required</h2>');
}
if (!function_exists('menu_set_active_item')) {
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
}
menu_set_active_item($path403);
return menu_execute_active_handler($path403);
}
/**
* Generate a node containing the standard "Access Denied" message
*
* @param $node
* The node object, whose teaser or body is modified to contain the message
* @param $teaser
* TRUE if the node's teaser should contain the message, otherwise the body
* is set
*/
function mm_node_load_fail(&$node, $teaser=FALSE) {
$node->content = array();
if ($teaser) {
$node->content['teaser']['#value'] = mm_access_denied();
$node->content['body']['#value'] = '';
}
else {
$node->content['teaser']['#value'] = '';
$node->content['body']['#value'] = mm_access_denied();
}
$node->no_attribution = TRUE;
$node->title = '';
}
/**
* Retrieve a string of autocomplete suggestions for existing users
*/
function mm_autocomplete($want_username, $string = '', $misc = NULL) {
$limit = 15;
$min_string = 2;
$matches = array();
$too_short = array('' => t('Please type some more characters'));
$string = trim($string);
if (!empty($string)) {
$hook = mm_module_implements('mm_autocomplete_alter');
if ($hook) {
$function = $hook[0] . '_mm_autocomplete_alter';
$result = $function($string, $limit, $min_string, $misc);
if (empty($result)) {
$matches = $too_short;
}
}
else if (drupal_strlen($string) >= $min_string) {
// Consider Anonymous and Administrator first
for ($i = 0; $i <= 1; $i++) {
$name = mm_content_uid2name($i);
if (($pos = stristr($name, $string)) !== FALSE) {
$stmt = "(SELECT $i, '', '', '$name', '', '', '', 0, 0, 0) UNION ";
if (!$pos) $startswith .= $stmt;
else $contains .= $stmt;
}
}
$status_limit = user_access('administer all users') ? '' : ' status = 1 AND';
$contains = preg_replace('/ UNION $/', '', $contains);
$result = db_query(
$startswith .
$contains .
"(SELECT uid, '' AS pref_fml, '' AS pref_lfm, '' AS lastname, '' AS firstname, name, '' AS middlename ".
"FROM {users} WHERE$status_limit uid > 0 AND name LIKE '%s%%' ".
'ORDER BY name) UNION '.
"(SELECT uid, '' AS pref_fml, '' AS pref_lfm, '' AS lastname, '' AS firstname, name, '' AS middlename ".
"FROM {users} WHERE$status_limit uid > 0 AND name LIKE '%%_%s%%' ".
'ORDER BY name) '.
'LIMIT %d',
$string, $string, $limit + 1);
}
else $matches = $too_short;
while ($user = db_fetch_object($result))
if (count($matches) == $limit) {
$matches[''] = '...';
break;
}
else {
$name = check_plain(mm_content_uid2name($user->uid, 'lfmu', $user));
if (!$want_username) $matches[$user->uid . '-' . $name] = $name;
else if ($user->name) $matches[$user->name] = $name;
}
}
drupal_set_header('Content-Type: text/plain');
print drupal_to_js($matches);
$GLOBALS['devel_shutdown'] = FALSE;
exit();
}
/**
* Redirect the user to a URL, while checking for possible recursion
*
* @param $url
* URL to redirect to
* @param $query
* Optional query fragment
* @param $hash
* Optional anchor to appear in the URL after '#'
*/
function mm_goto($url, $query = '', $hash = NULL) {
$u = $url;
custom_url_rewrite_outbound($u, $dummy, $url);
if ($url == $_GET['q'] || $u == $_GET['q'] || mm_home_path() . "/$u" == $_GET['q']) {
watchdog('mm', 'Recursive redirect: page=%page',
array('%page' => $_GET['q']), WATCHDOG_ERROR, l(t('view'), $_GET['q']));
menu_set_active_item('');
drupal_set_title(t('Error'));
print theme('page', t('This page tried to send you into an endless loop. Please contact the administrator, and let him or her know how you got here. Press your browser\'s Back button to return to the page you came from.'));
return;
}
drupal_goto($url, $query, $hash);
}
/**
* Return an <img> tag, or URL to an image, containing a user's email address in
* a form not easily grabbed by spambots
*
* @param $string
* The text to encode
* @param $alt
* The alternate text portion of the <img> tag
* @param $url_only
* If TRUE, return the URL, rather than the <img> tag
* @return
* Either the <img> tag or the URL
*/
function mm_text2image($string, $alt, $url_only = FALSE) {
$md5 = md5($string);
$_SESSION['mm_txtimg'][$md5] = $string;
$url = "mm-txtimg/$md5/x.jpg";
if ($url_only) return $url;
return '<img ' . drupal_attributes(array('src' => url($url), 'alt' => $alt)) . '>';
}
/**
* Return the actual image data containing a user's email address in a form not
* easily grabbed by spambots. A given image may only be accessed once per
* session.
*
* @param $md5
* If set, and there is a corresponding string set by mm_text2image(), output
* the image data
*/
function mm_text2image_img($md5 = NULL) {
if (isset($md5) && isset($_SESSION['mm_txtimg'][$md5])) {
$GLOBALS['devel_shutdown'] = TRUE;
list($fonts, $images) = mm_dep_add_libs('php-captcha');
$string = $_SESSION['mm_txtimg'][$md5];
$cap = new AmhCaptcha($fonts);
$cap->SetBackgroundImages($images);
$cap->SetMinFontSize(10);
$cap->SetMaxFontSize(10);
$cap->Create($string);
unset($_SESSION['mm_txtimg'][$md5]);
}
else drupal_not_found();
}
/**
* Parse GET parameters in URL
*
* @param &$mmtids
* Array to receive the list of tree IDs
* @param &$oarg_list
* Optional array to receive the parameters following the tree IDs, un-parsed
* @param &$this_mmtid
* Optional variable to receive the last tree ID (that of the current page)
* @param $url
* URL to parse; defaults to the current page URL
* @return
* The first GET parameter, usually 'mm'
*/
function mm_parse_args(&$mmtids, &$oarg_list = NULL, &$this_mmtid = NULL, $url = NULL) {
if (is_null($url)) $url = $_GET['q'];
$this_mmtid = NULL;
$mmtids = explode('/', $url);
$oarg_list = array();
if (($out = array_shift($mmtids)) == 'mm') { // skip 'mm'
for ($i = 0; $i < count($mmtids); $i++)
if (!is_numeric($mmtids[$i])) {
$oarg_list = array_splice($mmtids, $i);
break;
}
if (count($mmtids)) {
$this_mmtid = $mmtids[count($mmtids) - 1];
$parents = mm_content_get_parents($this_mmtid);
array_shift($parents); // skip root node
array_splice($mmtids, 0, -1, $parents); // insert parents
}
}
else {
$oarg_list = $mmtids;
$mmtids = array();
}
return $out;
}
/**
* Get the internal URI of the homepage
*
* @return
* The URI
*/
function mm_home_path() {
return 'mm/' . mm_home_mmtid();
}
/**
* Get the MM Tree ID of the homepage
*
* @return
* The MM Tree ID
*/
function mm_home_mmtid() {
return variable_get('mm_home_mmtid', MM_HOME_MMTID_DEFAULT);
}
/**
* Show a page, based on the GET parameters in the URL
*
* @return
* HTML code for the page
*/
function mm_show_page() {
$output = '';
mm_parse_args($mmtids, $oarg_list, $this_mmtid);
if (!count($mmtids)) $mmtids = array($this_mmtid = mm_home_mmtid());
$perms = mm_content_user_can($this_mmtid);
$output = _mm_render_pages($mmtids, $oarg_list, $err);
$links = array();
if ($perms['IS_RECYCLED']) {
if ($perms['IS_RECYCLE_BIN']) {
$recyc_msg = t('The contents below are in the recycle bin.');
}
else {
$recyc_msg = t('This page is in the recycle bin.');
if (mm_content_recycle_enabled()) {
$when = db_result(db_query("SELECT recycle_date FROM {mm_recycle} WHERE type = 'cat' AND id = %d", $this_mmtid));
$recyc_msg .= mm_content_get_recycle_autodel_time($when, NULL, $this_mmtid, t(' It'));
}
if ($perms['w'])
if (count($mmtids) >= 2 && mm_content_user_can($mmtids[count($mmtids)-2], 'IS_RECYCLE_BIN')) {
$recyc_msg .= t(' You can restore it using the %settings tab.', array('%settings' => t('Settings')));
}
else {
foreach (array_reverse($mmtids) as $t)
if (mm_content_user_can($t, 'IS_RECYCLE_BIN')) {
if ($last_t && ($tree = mm_content_get($last_t)))
$pg = l(mm_content_expand_name($tree->name), mm_content_get_mmtid_url($last_t));
break;
}
else $last_t = $t;
$recyc_msg .= t('<p>This page cannot be restored by itself. You must restore the topmost parent page in the recycle bin, !page.</p>', array('!page' => $pg));
}
}
}
if (!$err) {
if ($perms['IS_RECYCLED']) drupal_set_message($recyc_msg);
}
else if ($err == 'no read') {
$output .= $perms['IS_GROUP'] ? t('You do not have permission to see the members of this group.') : mm_access_denied();
}
else { // $err=='no content'
if (!$perms['IS_GROUP'] && !variable_get('mm_hide_empty_pages', FALSE)) {
global $user;
$list = array();
$entry = mm_content_get($this_mmtid, array(MM_GET_FLAGS, MM_GET_PARENTS));
if ($perms['IS_USER'] && isset($entry->flags['user_home'])) {
if ($entry->flags['user_home'] == $user->uid) {
$list[0] = variable_get('mm_default_homepage', '');
}
if (empty($list[0])) {
$list[0] = t('<h2>Welcome</h2><p>This is a personal homepage that has not been modified yet.</p>');
}
}
else if ($perms['IS_RECYCLE_BIN'])
$list[0] = ' ';
else
$list[0] = t('<p>This page does not yet have any content.</p>');
$entry->perms = $perms;
mm_module_invoke_all_array('mm_empty_page_alter', array($entry, &$list));
$nada = join('', $list);
}
else $nada = ' ';
if ($perms['u'] && !$perms['IS_RECYCLED'] && !$perms['IS_GROUP']) {
$link = "mm/$this_mmtid/contents/add";
if (!$perms['IS_GROUP'] && !variable_get('mm_hide_empty_pages', FALSE)) {
$links[] = array(
'title' => t('Add content'),
'href' => $link,
);
}
else {
mm_goto($link);
return;
}
}
else if ($perms['IS_GROUP']) {
$users = mm_content_get_users_in_group($this_mmtid, NULL, FALSE, 100, TRUE);
$node = (object)array();
if (!count($users)) $node->title = t('There are no users in this group.');
else if ($users[''] == '...') {
$node->title = t('A partial list of users in this group:');
}
else {
$node->title = t('All users in this group:');
}
$node->body = join('<br />', $users);
$node->type = 'story';
$output = theme('node', $node, FALSE, TRUE);
}
else if ($perms['w'] || $perms['a'])
if (!$perms['IS_GROUP'] && !variable_get('mm_hide_empty_pages', FALSE)) {
if (!$perms['IS_RECYCLED'])
$nada .= t('<p>You do not have permission to add content, however you can use the %settings tab to make changes to the page itself.</p>', array('%settings' => t('Settings')));
}
else {
mm_goto("mm/$this_mmtid/settings");
return;
}
if ($perms['IS_RECYCLED'])
if ($perms['IS_RECYCLE_BIN'])
drupal_set_message(t('Select an item in the recycle bin from the menu.'));
else
drupal_set_message($recyc_msg);
if ($output == '') {
if (!isset($node)) $node = (object)array();
$node->body = $nada;
$node->links = $links;
unset($links);
$node->type = 'story';
$output = theme('node', $node, FALSE, TRUE);
}
} // $err=='no content'
if ($links) {
$output .= theme('links', $links);
}
return $output;
}
function mm_show_group($this_mmtid) {
drupal_set_header('Content-Type: text/html');
print '<pre>' . mm_content_get_users_in_group($this_mmtid, "\n", FALSE, 0) . '</pre>';
$GLOBALS['devel_shutdown'] = FALSE; // prevent the devel module from outputting
exit();
}
/**
* Return a list of node types for which a flag is TRUE in the
* hook_mm_node_info()
*
* @param $option
* The flag to test. Should be one of the MM_NODE_INFO_* constants.
* @return
* An array of node types
*/
function mm_get_node_info($option = NULL) {
static $list;
if (!is_array($list)) {
$list = array();
foreach (module_invoke_all('mm_node_info') as $node_type => $data)
foreach ($data as $field => $value)
if ($value === TRUE)
$list[$field][] = $node_type;
}
if (empty($option)) return $list;
return isset($list[$option]) ? $list[$option] : array();
}
/**
* Set the theme based on the page being displayed
*/
function mm_set_custom_theme() {
global $custom_theme;
mm_parse_args($mmtids, $oarg_list);
if (count($mmtids)) {
$place = db_placeholders($mmtids);
if ($theme = db_result(db_query_range('SELECT theme FROM {mm_tree} WHERE mmtid IN(' . $place . ") AND theme <> '' ORDER BY LENGTH(sort_idx) DESC", $mmtids, 0, 1))) {
$custom_theme = $theme;
}
}
}
/**
* When the requested URL is not found, evaluate it and try to come up with some
* possible guesses as to what the user mis-typed, or to where the intended page
* has moved.
*
* @return
* HTML code containing the possible guesses. If an empty string is returned,
* the calling code should generate a default "page not found" message.
*/
function mm_get_detailed_404() {
global $user;
$max_results = 10;
$skipped = FALSE;
$bad_path = explode('/', $_REQUEST['q']);
if ($site_404 = variable_get('site_404', '')) {
custom_url_rewrite_inbound($site_404, $site_404, NULL);
}
$new_path = '';
custom_url_rewrite_inbound($new_path, implode('/', $bad_path), NULL);
if ($new_path == $site_404) return '';
// Set the custom_theme to that of the 404 page, instead of the original page
mm_set_custom_theme();
$home_mmtid = mm_home_mmtid();
$new_path = explode('/', $new_path);
if ($new_path[0] == 'mm') {
$found_mmtid = $new_path[1];
$new_path = array_slice($new_path, 2);
}
else {
$found_mmtid = $home_mmtid;
}
$mtime = 0;
$output_list = array();
$prefix_single = t('<p>The page you are looking for appears to have moved to: ');
while ($child = array_shift($new_path)) {
$use_mtime = $mtime > 0 ? ' AND mtime <= %d' : '';
if (db_result(db_query("SELECT COUNT(*) FROM {mm_tree_revisions} r LEFT JOIN {mm_tree} t ON t.mmtid = r.mmtid WHERE r.parent = %d AND r.alias = '%s' AND t.mmtid IS NULL", $found_mmtid, $child))) {
$prefix_single = t('<p>The page you requested has been permanently deleted.');
if (!count($output_list) && $found_mmtid != $home_mmtid) {
$output_list[0] = $found_mmtid;
}
if (count($output_list)) {
$prefix_single .= t(' You may be able to find what you were looking for here: ');
}
else {
return $prefix_single;
}
}
else if ($row = db_fetch_object(db_query("SELECT mmtid, mtime FROM {mm_tree_revisions} WHERE parent = %d AND alias = '%s' AND LEFT(name, 1) <> '%s'$use_mtime ORDER BY vid DESC", $found_mmtid, $child, '.', $mtime))) {
if (!mm_content_user_can($row->mmtid, 'r')) {
$output_list = array();
$skipped = TRUE;
break;
}
$output_list[0] = $found_mmtid = $row->mmtid;
$mtime = $row->mtime;
}
else {
$output_list = array();
// Use the SQL SOUNDEX() function instead of the PHP soundex() version,
// because they produce different values
$soundex = _mm_soundex($child);
$soundex_short = substr($soundex, 1, 3);
$queries = array(
// hook_menu() paths
'hook' => array("SELECT * FROM {menu_router} WHERE SUBSTR(path, 1, 4) <> '%s' AND (SUBSTRING_INDEX(path, '/', 1) = '%s' OR SOUNDEX(SUBSTRING_INDEX(path, '/', 1)) = '%s') ORDER BY fit DESC", 'mm/%', $child, $soundex),
// Direct alias match at the correct level of the tree
'p+a+' => array("t.parent = %d AND t.alias = '%s'", $found_mmtid, $child),
// Alias starts with the string, at the correct level
'p+a*' => array("t.parent = %d AND t.alias LIKE '%s'", $found_mmtid, $child . '%'),
// Alias sounds like the string, at the correct level
'p+as' => array("t.parent = %d AND SOUNDEX(t.alias) = '%s'", $found_mmtid, $soundex),
// Alias matches at any level
'p-a+' => array("t.alias = '%s'", $child),
// Alias sounds like the string, at any level
'p-as' => array("t.parent <> %d AND SOUNDEX(t.alias) = '%s'", $found_mmtid, $soundex),
// Alias sounds like the string, at any level, using minimal match
'p<as' => array("t.parent <> %d AND SUBSTR(SOUNDEX(t.alias), 2, 3) = '%s'", $found_mmtid, $soundex_short),
);
$soundex_bad = FALSE;
foreach ($queries as $index => $params) {
if ($index == 'p+as') {
// See if there is enough variance in the soundex value, by checking
// the number of consonants and the frequency of resulting digits.
$len_test = preg_replace('/[^bcdfghjklmnpqrstvwxyz]/i', '', $child);
if (strlen($len_test) < 3 || strlen(count_chars($soundex, 3)) / strlen($soundex) <= 2/3) {
$soundex_bad = TRUE;
continue;
}
}
else if ($index == 'p-a+') {
if (count($output_list) == 1) {
$found_mmtid = $output_list[0];
break;
}
}
else if ($index == 'p-as' || $index == 'p<as') {
// This is the last-ditch effort, only if everything before has
// failed, and the soundex value is sufficiently unique
if (count($output_list) || $soundex_bad) break;
$max_results = 5;
}
else if ($index == 'hook') {
// Search in the hook_menu() list for a close match, using just the
// first element of the path. Only do this when MM found no match at
// all.
if ($found_mmtid != $home_mmtid) continue;
$test_path = $new_path;
array_unshift($test_path, $child);
$query = array_shift($params);
$results = db_query($query, $params);
while ($menu = db_fetch_object($results)) {
$matched = TRUE;
$match = array();
foreach (explode('/', $menu->path) as $path_index => $elem) {
if ($path_index >= count($test_path)) {
$matched = FALSE;
break;
}
if ($elem == '%' || strcasecmp($test_path[$path_index], $elem) == 0) {
$match[] = $test_path[$path_index];
}
else if (_mm_soundex($test_path[$path_index]) == _mm_soundex($elem)) {
$match[] = $elem;
}
else {
$matched = FALSE;
break;
}
}
if ($matched) {
$item = (array)$menu;
_menu_translate($item, $match, TRUE);
if ($item['access']) {
$output_list[] = implode('/', $match);
}
}
}
if (count($output_list)) {
// Stop looking.
$new_path = array();
}
continue; // Go to next query.
}
$where = array_shift($params);
// There's no need to do matches against mm_tree, since its data is
// duplicated in mm_tree_revisions in the most recent revision.
// Don't match items or parents with a name starting with '.'.
$query = "SELECT DISTINCT t.mmtid FROM {mm_tree_revisions} t WHERE $where AND LEFT(t.name, 1) <> '%s' AND (SELECT COUNT(*) FROM {mm_tree} t2 INNER JOIN {mm_tree_parents} p ON p.parent = t2.mmtid WHERE p.mmtid = t.mmtid AND LEFT(t2.name, 1) = '%s') = 0";
$params[] = '.';
$params[] = '.';
// Skip mmtids already found
if (count($output_list)) {
$query .= ' AND t.mmtid NOT IN (' . db_placeholders($output_list) . ')';
$params = array_merge($params, $output_list);
}
$results = db_query($query . ' ORDER BY t.vid DESC', $params);
while (count($output_list) < $max_results && ($item = db_fetch_object($results)))
if (mm_content_user_can($item->mmtid, 'r')) {
$output_list[] = $item->mmtid;
}
else {
$skipped = TRUE;
}
if (count($output_list) >= $max_results) break;
}
if (!$output_list) {
if ($found_mmtid == $home_mmtid) return '';
$output_list[0] = $found_mmtid;
$prefix_single = t('<p>The page you are looking for was not found, but you might be able to find it here: ');
break;
}
$prefix_single = t('<p>This page might be what you are looking for: ');
$prefix_many = t('<p>One of these pages might be what you are looking for:<ul><li>');
if (count($output_list) != 1) break;
}
}
if (count($output_list) == 1 && is_numeric($output_list[0]) && mm_content_is_recycled($output_list[0])) {
// We'll only get here if the page is readable by the user
return t('<p>The page you requested has been marked for future deletion. Therefore, it is no longer accessible.</p>');
}
$options = array();
foreach ($output_list as $index => $elem) {
$path = $path0 = 'mm/' . $elem;
custom_url_rewrite_outbound($path, $options, NULL);
if ($path == $path0) {
unset($output_list[$index]);
}
else {
$output_list[$index] = l(base_path() . $path, $path, array('attributes' => array('rel' => 'nofollow')));
}
}
$post = !$user->uid && $skipped ? t('<p>You might get more results if you log-in.</p>') : '';
if (count($output_list) == 1) {
return $prefix_single . $output_list[0] . '</p>' . $post;
}
if (count($output_list) > 0) {
return $prefix_many . join('</li><li>', $output_list) . '</li></ul></p>' . $post;
}
return !$user->uid && $skipped ? t('This page may be able to provide you with suggestions as to where to find what you are looking for, but you need to log-in first.') : '';
}
/**
* Redirect the user to an MM URL based on a Drupal node
*
* @param $nid
* Drupal node ID to redirect to
* @param $add
* Path elements to add after the last MM tree ID. If used, must start with '/'.
* @param $hash
* Optional anchor to appear in the URL after '#'
*/
function mm_redirect_to_node($nid, $add = NULL, $hash = NULL) {
if ($mmtid = db_result(db_query_range('SELECT mmtid FROM {mm_node2tree} WHERE nid = %d', $nid, 0, 1))) {
mm_redirect_to_mmtid($mmtid, $add, $hash);
}
}
/**
* Redirect the user to an MM URL based on an MM tree ID
*
* @param $mmtid
* Tree ID to redirect to
* @param $add
* Path elements to add after the last MM tree ID. If used, must start with '/'.
* @param $hash
* Optional anchor to appear in the URL after '#'
*/
function mm_redirect_to_mmtid($mmtid, $add = NULL, $hash = NULL) {
mm_goto("mm/$mmtid$add", NULL, $hash);
}
/**
* Add MM group-based roles to user objects being loaded
*/
function mm_set_user_roles(&$account) {
global $user;
$added_role = FALSE;
$q = mm_query(
'SELECT rid, name FROM (SELECT r2.rid, r.name, (COALESCE(SUM(v.uid = %d OR g.vgid = 0 AND g.uid = %d), 0) > 0) <> r2.negative AS ok FROM {mm_role2group} r2 INNER JOIN {role} r ON r.rid = r2.rid INNER JOIN {mm_group} g ON g.gid = r2.gid LEFT JOIN {mm_virtual_group} v ON g.vgid = v.vgid GROUP BY r2.rid) AS s WHERE s.ok', $account->uid, $account->uid);
while ($r = $q->next()) {
if (!isset($account->roles[$r->rid])) {
$account->roles[$r->rid] = $r->name;
$added_role = TRUE;
}
}
if ($account->uid == $user->uid && $added_role) {
$user->roles = $account->roles;
if (function_exists('user_access')) {
// Clear out the user_access cache
user_access('', NULL, TRUE);
}
}
}
/**
* Update the table containing all results of virtual group queries.
*/
function mm_regenerate_vgroup() {
// delete entries for groups that no longer exist
db_query('DELETE FROM v USING {mm_virtual_group} v LEFT JOIN {mm_vgroup_query} q ON v.vgid=q.vgid WHERE q.vgid IS NULL');
$vgids = $email_errors = array();
// split the queries up into chunks of mm_vgroup_regen_chunk, so that the SQL
// buffer size isn't exceeded
$chunksize = variable_get('mm_vgroup_regen_chunk', 15);
$chunks_per_run = variable_get('mm_vgroup_regen_chunks_per_run', 50);
for ($chunk = 0; $chunk < $chunks_per_run; $chunk++) {
$i = $chunksize * $chunk;
$list = array();
// dirty==MM_VGROUP_DIRTY_FAILED means there was a previous sanity error, so
// ignore it now
$result = db_query_range('SELECT * FROM {mm_vgroup_query} WHERE dirty IN(%d, %d)', MM_VGROUP_DIRTY_NEXT_CRON, MM_VGROUP_DIRTY_REDO, $i, $chunksize);
$nrows = 0;
$chunk_vgids = array();
while ($r = db_fetch_object($result)) {
$list[] = "(SELECT $r->vgid, $r->field $r->qfrom)";
$vgids[] = $r->vgid;
$chunk_vgids[] = $r->vgid;
$nrows++;
}
if (!$nrows) break;
if (!$created) {
// db_query_temporary() isn't flexible enough, because we need an index
@db_drop_table($create_errs, 'mm_virtual_group_temp');
db_create_table($create_errs, 'mm_virtual_group_temp', drupal_get_schema('mm_virtual_group'));
if (!$create_errs[1]['success']) {
watchdog('mm', 'Could not create table mm_virtual_group_temp',
array(), WATCHDOG_ERROR);
return;
}
$created = TRUE;
}
db_query('INSERT INTO {mm_virtual_group_temp} (vgid, uid) ' . join(' UNION ', $list));
// Delete uids that no longer exist in users table
db_query('DELETE vg FROM {mm_virtual_group_temp} vg LEFT JOIN {users} u ON u.uid = vg.uid WHERE u.uid IS NULL');
$in = join(',', $chunk_vgids);
$result = db_query('SELECT vgid1 AS vgid, gid, orig_count, IFNULL(temp_count, 0) AS temp_count ' .
'FROM (' .
'SELECT * FROM (' .
'SELECT vgid AS vgid1, COUNT(*) AS orig_count ' .
'FROM {mm_virtual_group} ' .
'WHERE vgid IN(' . $in . ') ' .
'GROUP BY vgid) ' .
'AS t1 ' .
'LEFT JOIN (' .
'SELECT vgid AS vgid2, COUNT(*) AS temp_count ' .
'FROM {mm_virtual_group_temp} ' .
'WHERE vgid IN(' . $in . ') ' .
'GROUP BY vgid) ' .
'AS t2 ' .
'ON vgid2 = vgid1 ' .
'WHERE IFNULL(temp_count, 0) < orig_count AND (orig_count - IFNULL(temp_count, 0)) / orig_count > ' . MM_VGROUP_COUNT_SANITY . ' AND (temp_count IS NULL OR temp_count >= 10)) ' .
'AS insane ' .
'INNER JOIN {mm_vgroup_query} vg ON vg.vgid = insane.vgid1 ' .
'LEFT JOIN {mm_group} g ON g.vgid = insane.vgid1 ' .
'WHERE vg.dirty <> %d', MM_VGROUP_DIRTY_REDO);
while ($r = db_fetch_object($result)) {
$tree = mm_content_get($r->gid);
$msg = t('The size of the virtual group with vgid=!vgid, gid=!gid, name=!name went down by more than !pct%. It went from !orig to !new users. To ignore this condition and regenerate the virtual group anyway, set its "dirty" field to !redo.', array('!vgid' => $r->vgid, '!gid' => $r->gid, '!name' => $tree->name, '!pct' => MM_VGROUP_COUNT_SANITY*100, '!orig' => $r->orig_count, '!new' => $r->temp_count, '!redo' => MM_VGROUP_DIRTY_REDO));
$email_errors[$r->vgid] = $msg;
watchdog('mm', $msg, array(), WATCHDOG_ERROR);
// Set dirty to MM_VGROUP_DIRTY_FAILED so that the same error is not
// logged repeatedly
db_query('UPDATE {mm_vgroup_query} SET dirty = %d WHERE vgid = %d', MM_VGROUP_DIRTY_FAILED, $r->vgid);
// Don't copy data from temp to real table for this vgid
$vgid_index = array_search($r->vgid, $vgids);
if ($vgid_index !== FALSE) array_splice($vgids, $vgid_index, 1);
}
if ($nrows < $chunksize) break;
}
if (count($vgids)) {
// the anonymous user can never be in any groups
db_query('DELETE FROM {mm_virtual_group_temp} WHERE uid = 0');
// update the preview column for mm_content_get_users_in_group()
$result = db_query('SELECT vgid FROM {mm_virtual_group_temp} GROUP BY vgid');
while ($r = db_fetch_object($result)) {
db_query('SET @i=0');
$query =
'UPDATE {mm_virtual_group_temp} t '.
'INNER JOIN (' .
'SELECT v.uid, (@i:=@i+1) AS ind FROM {mm_virtual_group_temp} v '.
'INNER JOIN {users} u ON v.vgid = %d AND u.uid = v.uid '.
'ORDER BY u.name' .
') AS j '.
'ON j.uid = t.uid AND t.vgid = %d SET preview = j.ind';
mm_module_invoke_all_array('mm_regenerate_vgroup_preview_alter', array(&$query));
db_query($query, $r->vgid, $r->vgid);
}
$in = join(',', $vgids);
db_query('INSERT INTO {mm_virtual_group} '.
'(SELECT t.vgid, t.uid, t.preview '.
'FROM {mm_virtual_group_temp} t LEFT JOIN {mm_virtual_group} o '.
'ON t.vgid = o.vgid AND t.uid = o.uid '.
'WHERE o.uid IS NULL)') &&
db_query('UPDATE {mm_virtual_group} v '.
'INNER JOIN {mm_virtual_group_temp} n ON '.
'v.vgid = n.vgid AND v.uid = n.uid '.
'SET v.preview = n.preview') &&
db_query('DELETE o FROM {mm_virtual_group} o '.
'LEFT JOIN {mm_virtual_group_temp} t '.
'ON t.vgid = o.vgid AND t.uid = o.uid '.
"WHERE t.uid IS NULL AND o.vgid IN($in)") &&
db_query('UPDATE {mm_vgroup_query} '.
"SET dirty = %d WHERE vgid IN($in)", MM_VGROUP_DIRTY_NOT);
}
if ($email_errors) {
$in = join(',', array_keys($email_errors));
$sql = "\n\nTo ignore the errors and regenerate the data in all of the listed virtual groups, execute this SQL code:\n\nUPDATE mm_vgroup_query SET dirty=" . MM_VGROUP_DIRTY_REDO . " WHERE vgid IN($in)";
drupal_mail_send(array('id' => 'mm_regenerate_vgroup', 'to' => variable_get('academics_sync_reports_to_email', ''),
'subject' => t('Error in virtual group regeneration'), 'body' => join("\n\n", array_values($email_errors)) . $sql, 'headers' => array()));
}
if ($created) db_drop_table($create_errs, 'mm_virtual_group_temp');
db_query('OPTIMIZE TABLE {mm_virtual_group}');
return 'Virtual groups have been regenerated.';
}
/**
* @defgroup mm_hooks Monster Menus Hooks
* @{
* Allow modules to interact with Monster Menus.
*/
/**
* Get a list of all enabled modules and MM sub-modules that implement a hook.
*
* @param $hook
* The name of the hook to query
* @return
* An array of module names
*/
function mm_module_implements($hook) {
$list = module_implements($hook);
foreach (monster_menus_node_info() as $type => $desc)
if (function_exists($desc['module'] . '_' . $hook)) $list[] = $desc['module'];
return $list;
}
/**
* Invoke a hook in all enabled modules and MM sub-modules that implement it.
*
* @param $hook
* The name of the hook to invoke.
* @param ...
* Arguments to pass to the hook.
* @return
* An array of return values of the hook implementations. If modules return
* arrays from their implementations, those are merged into one array.
*/
function mm_module_invoke_all() {
$args = func_get_args();
$hook = array_shift($args);
$return = array();
foreach (mm_module_implements($hook) as $module) {
$function = $module .'_'. $hook;
$result = call_user_func_array($function, $args);
if (isset($result) && is_array($result)) {
$return = array_merge_recursive($return, $result);
}
else if (isset($result)) {
$return[] = $result;
}
}
return $return;
}
/**
* Invoke a hook in all enabled modules and MM sub-modules that implement it.
* Unlike mm_module_invoke_all(), any references are preserved.
*
* @param $hook
* The name of the hook to invoke.
* @param $args
* Arguments to pass to the hook. Any references in the array are preserved.
* @return
* An array of return values of the hook implementations. If modules return
* arrays from their implementations, those are merged into one array.
*/
function mm_module_invoke_all_array($hook, $args) {
$return = array();
foreach (mm_module_implements($hook) as $module) {
$function = $module .'_'. $hook;
$result = call_user_func_array($function, $args);
if (isset($result) && is_array($result)) {
$return = array_merge_recursive($return, $result);
}
else if (isset($result)) {
$return[] = $result;
}
}
return $return;
}
/**
* @} End of "defgroup mm_hooks".
*/
// ****************************************************************************
// * Private functions start here
// ****************************************************************************
function _mm_render_pages($mmtids, $oarg_list, &$err, $no_attribution = FALSE, $allow_rss = TRUE, $block_id = 0) {
global $user, $_mm_page_subscribe_item, $_mm_mmtid_of_node;
$err = '';
$this_mmtid = $mmtids[count($mmtids) - 1];
$no_read = 0; $ok = 0;
// Check for mm_showpage callbacks, specified in hook_mm_showpage_routing()
$showpage_no_nodes = FALSE;
$router = _mm_showpage_router();
if ($router) {
$temp_path = "mm/$this_mmtid";
custom_url_rewrite_outbound($temp_path, $temp_options, $temp_path);
$temp_path = join('/', array_merge(array($temp_path), $oarg_list));
$temp_args = split('/', $temp_path);
foreach ($router as $key => $item) {
if (preg_match($key, $temp_path) && (!isset($item['block id']) || $item['block id'] == $block_id) && _mm_showpage_callback($item, 'access', $temp_args, $this_mmtid, $block_id)) {
$showpage_output = _mm_showpage_callback($item, 'page', $temp_args, $this_mmtid, $block_id);
if (is_array($showpage_output)) {
$output .= $showpage_output['output_pre'];
$output_post .= $showpage_output['output_post'];
$showpage_no_nodes |= $showpage_output['no_nodes'];
}
else if ($showpage_output) {
$output .= $showpage_output;
$showpage_no_nodes = FALSE;
}
}
}
}
$showpage_show_nodes = !$showpage_no_nodes;
$nodes_per_page = mm_content_resolve_cascaded_setting('nodes_per_page', $this_mmtid, $npp_at, $npp_parent);
if (empty($nodes_per_page)) $nodes_per_page = variable_get('default_nodes_main', 10);
$perms = mm_content_user_can($this_mmtid);
if (!$perms['r']) {
$no_read++;
}
else if ((count($mmtids) == 1 || count($mmtids) == 2 && variable_get('mm_use_virtual_user_dir', TRUE)) && $mmtids[0] == mm_content_users_mmtid()) {
module_load_include('inc', 'monster_menus', 'mm_ui_user_list');
$output .= mm_ui_user_list_form($mmtids);
$ok++;
}
else if ($showpage_show_nodes) {
// display a list of pages assigned to the current tree entry
$item = mm_content_get($this_mmtid, MM_GET_ARCHIVE);
$result = NULL;
$omit_nodes = '';
if (!$perms['IS_RECYCLED']) {
$omit_node_types = mm_get_node_info(MM_NODE_INFO_NO_RENDER);
if ($omit_node_types) $omit_nodes = " AND n.type NOT IN('" . join("', '", $omit_node_types) . "')";