summaryrefslogtreecommitdiffstats
path: root/devel.module
blob: fddd7b416cfb5473f367732f47c18ed2b75d5293 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
<?php

// This module holds functions useful for Drupal development.
// Please contribute!

// Suggested profiling and stacktrace library from http://www.xdebug.org/index.php

define('DEVEL_QUERY_SORT_BY_SOURCE', 0);
define('DEVEL_QUERY_SORT_BY_DURATION', 1);

define('DEVEL_ERROR_HANDLER_NONE', 0);
define('DEVEL_ERROR_HANDLER_STANDARD', 1);
define('DEVEL_ERROR_HANDLER_BACKTRACE', 2);

define('DEVEL_MIN_TEXTAREA', 50);

/**
 * Implementation of hook_help().
 */
function devel_help($section) {
  switch ($section) {
    case 'devel/reference':
      return '<p>'. t('This is a list of defined user functions that generated this current request lifecycle. Click on a function name to view its documention.') .'</p>';
    case 'devel/session':
      return '<p>'. t('Here are the contents of your <code>$_SESSION</code> variable.') .'</p>';
    case 'devel/variable':
      $api = variable_get('devel_api_url', 'api.drupal.org');
      return '<p>'. t('This is a list of the variables and their values currently stored in variables table and the <code>$conf</code> array of your settings.php file. These variables are usually accessed with <a href="@variable-get-doc">variable_get()</a> and <a href="@variable-set-doc">variable_set()</a>. Variables that are too long can slow down your pages.', array('@variable-get-doc' => "http://$api/api/HEAD/function/variable_get", '@variable-set-doc' => "http://$api/api/HEAD/function/variable_set")) .'</p>';
    case 'devel/reinstall':
      return t('Warning - will delete your module tables and variables.');
  }
}

/**
 * Implementationation of hook_menu().
 */
function devel_menu() {
  $items = array();
  // Note: we can't dynamically append destination to querystring. Do so at theme layer. Fix in D7?
  $items['devel/cache/clear'] = array(
    'title' => 'Empty cache',
    'page callback' => 'devel_cache_clear',
    'description' => 'Clear the CSS cache and all database cache tables which store page, node, theme and variable caches.',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );

  $items['devel/reference'] = array(
    'title' => 'Function reference',
    'description' => 'View a list of currently defined user functions with documentation links.',
    'page callback' => 'devel_function_reference',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/reinstall'] = array(
    'title' => 'Reinstall modules',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('devel_reinstall'),
    'description' => 'Run hook_uninstall() and then hook_install() for a given module.',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/menu/reset'] = array(
    'title' => 'Rebuild menus',
    'description' => 'Rebuild menu based on hook_menu() and revert any custom changes. All menu items return to their default settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('devel_menu_rebuild'),
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/menu/item'] = array(
    'title' => 'Menu item',
    'description' => 'Details about a given menu item.',
    'page callback' => 'devel_menu_item',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/variable'] = array(
    'title' => 'Variable editor',
    'description' => 'Edit and delete site variables.',
    'page callback' => 'devel_variable_page',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  // we don't want the abbreviated version provided by status report
  $items['devel/phpinfo'] = array(
    'title' => 'PHPinfo()',
    'description' => 'View your server\'s PHP configuration',
    'page callback' => 'devel_phpinfo',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/php'] = array(
    'title' => 'Execute PHP Code',
    'description' => 'Execute some PHP code',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('devel_execute_form'),
    'access arguments' => array('execute php code'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/theme/registry'] = array(
    'title' => 'Theme registry',
    'description' => 'View a list of available theme functions across the whole site.',
    'page callback' => 'devel_theme_registry',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/entity/info'] = array(
    'title' => 'Entity info',
    'description' => 'View entity information across the whole site.',
    'page callback' => 'devel_entity_info_page',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/field/info'] = array(
    'title' => 'Field info',
    'description' => 'View fields information across the whole site.',
    'page callback' => 'devel_field_info_page',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/elements'] = array(
    'title' => 'Hook_elements()',
    'description' => 'View the active form/render elements for this site.',
    'page callback' => 'devel_elements_page',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/variable/edit/%'] = array(
    'title' => 'Variable editor',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('devel_variable_edit', 3),
    'access arguments' => array('access devel information'),
    'type' => MENU_CALLBACK,
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/session'] = array(
    'title' => 'Session viewer',
    'description' => 'List the contents of $_SESSION.',
    'page callback' => 'devel_session',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/switch'] = array(
    'title' => 'Switch user',
    'page callback' => 'devel_switch_user',
    'access arguments' => array('switch users'),
    'type' => MENU_CALLBACK,
    'file' => 'devel.pages.inc',
    'menu_name' => 'devel',
  );
  $items['devel/explain'] = array(
    'title' => 'Explain query',
    'page callback' => 'devel_querylog_explain',
    'description' => 'Run an EXPLAIN on a given query. Used by query log',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'type' => MENU_CALLBACK
  );
  $items['devel/arguments'] = array(
    'title' => 'Arguments query',
    'page callback' => 'devel_querylog_arguments',
    'description' => 'Return a given query, with arguments instead of placeholders. Used by query log',
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'type' => MENU_CALLBACK
  );
  $items['devel/run-cron'] = array(
    'title' => 'Run cron',
    'page callback' => 'system_run_cron',
    'access arguments' => array('administer site configuration'),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
    'menu_name' => 'devel',
  );

  // Duplicate path in 2 different menus. See http://drupal.org/node/601788.
  $items['devel/settings'] = array(
    'title' => 'Devel settings',
    'description' =>  'Helper functions, pages, and blocks to assist Drupal developers. The devel blocks can be managed via the <a href="' . url('admin/structure/block') . '">block administration</a> page.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('devel_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'file' => 'devel.admin.inc',
    'menu_name' => 'devel',
  );
  $items['admin/config/development/devel'] = array(
    'title' => 'Devel settings',
    'description' =>  'Helper functions, pages, and blocks to assist Drupal developers. The devel blocks can be managed via the <a href="' . url('admin/structure/block') . '">block administration</a> page.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('devel_admin_settings'),
    'file' => 'devel.admin.inc',
    'access arguments' => array('administer site configuration'),
  );

  $items['node/%node/devel'] = array(
    'title' => 'Devel',
    'page callback' => 'devel_load_object',
    'page arguments' => array('node', 1),
    'access arguments' => array('access devel information'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'devel.pages.inc',
    'weight' => 100,
  );
  $items['node/%node/devel/load'] = array(
    'title' => 'Load',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['node/%node/devel/render'] = array(
    'title' => 'Render',
    'page callback' => 'devel_render_object',
    'page arguments' => array('node', 1),
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 100,
  );
  $items['comment/%comment/devel'] = array(
    'title' => 'Devel',
    'page callback' => 'devel_load_object',
    'page arguments' => array('comment', 1),
    'access arguments' => array('access devel information'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'devel.pages.inc',
    'weight' => 100,
  );
  $items['comment/%comment/devel/load'] = array(
    'title' => 'Load',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['comment/%comment/devel/render'] = array(
    'title' => 'Render',
    'page callback' => 'devel_render_object',
    'page arguments' => array('comment', 1),
    'access arguments' => array('access devel information'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'devel.pages.inc',
    'weight' => 100,
  );
  $items['user/%user/devel'] = array(
    'title' => 'Devel',
    'page callback' => 'devel_load_object',
    'page arguments' => array('user', 1),
    'access arguments' => array('access devel information'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'devel.pages.inc',
    'weight' => 100,
  );
  $items['user/%user/devel/load'] = array(
    'title' => 'Load',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['user/%user/devel/render'] = array(
    'title' => 'Render',
    'page callback' => 'devel_render_object',
    'page arguments' => array('user', 1),
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 100,
  );
  $items['taxonomy/term/%taxonomy_term/devel'] = array(
    'title' => 'Devel',
    'page callback' => 'devel_load_object',
    'page arguments' => array('taxonomy_term', 2, 'term'),
    'access arguments' => array('access devel information'),
    'file' => 'devel.pages.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 100,
  );
  $items['taxonomy/term/%taxonomy_term/devel/load'] = array(
    'title' => 'Load',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['taxonomy/term/%taxonomy_term/devel/render'] = array(
    'title' => 'Render',
    'page callback' => 'devel_render_object',
    'page arguments' => array('taxonomy_term', 2, 'term'),
    'access arguments' => array('access devel information'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'devel.pages.inc',
    'weight' => 100,
  );

  return $items;
}

/**
 * Implements hook_admin_paths().
 */
function devel_admin_paths() {
  $paths = array(
    'devel/*' => TRUE,
    'node/*/devel' => TRUE,
    'node/*/devel/*' => TRUE,
    'comment/*/devel' => TRUE,
    'comment/*/devel/*' => TRUE,
    'user/*/devel' => TRUE,
    'user/*/devel/*' => TRUE,
    'taxonomy/term/*/devel' => TRUE,
    'taxonomy/term/*/devel/*' => TRUE,
  );
  return $paths;
}

function devel_menu_need_destination() {
  return array('devel/cache/clear', 'devel/reinstall', 'devel/menu/reset', 'devel/variable', 'admin/reports/status/run-cron');
}

/**
 * An implementation of hook_menu_link_alter(). Flag this link as needing alter at display time.
 * This is more robust than setting alter in hook_menu().
 * @see devel_translated_menu_link_alter().
 *
 **/
function devel_menu_link_alter(&$item) {
  if (in_array($item['link_path'], devel_menu_need_destination()) || $item['link_path'] == 'devel/menu/item') {
    $item['options']['alter'] = TRUE;
  }
}

/**
 * An implementation of hook_translated_menu_item_alter(). Append dynamic
 * querystring 'destination' to several of our own menu items.
 *
 **/
function devel_translated_menu_link_alter(&$item) {
  if (in_array($item['href'], devel_menu_need_destination())) {
    $item['localized_options']['query'] = drupal_get_destination();
  }
  elseif ($item['href'] == 'devel/menu/item') {
    $item['localized_options']['query'] = array('path' => $_GET['q']);
  }
}

/**
 * Implementation of hook_theme()
 */
function devel_theme() {
  return array(
    'devel_querylog' => array(
      'variables' => array('header' => array(), 'rows' => array()),
    ),
    'devel_querylog_row' => array(
      'variables' => array('row' => array()),
    ),
  );
}

/**
 * Implementation of hook_init().
 */
function devel_init() {
  if (!devel_silent()) {
    if (user_access('access devel information')) {
      devel_set_handler(variable_get('devel_error_handler', DEVEL_ERROR_HANDLER_STANDARD));
      // We want to include the class early so that anyone may call krumo() as needed. See http://krumo.sourceforge.net/
      has_krumo();

      // See http://www.firephp.org/HQ/Install.htm
      $path = NULL;
      if (@include_once('fb.php')) {
        // FirePHPCore is in include_path. Probably a PEAR installation.
        $path = '';
      }
      elseif (module_exists('libraries')) {
        // Support Libraries API - http://drupal.org/project/libraries
        $firephp_path = libraries_get_path('FirePHPCore') . '/lib/FirePHPCore/';
        $chromephp_path = libraries_get_path('chromephp');
      }
      else {
        $firephp_path = './'. drupal_get_path('module', 'devel') .'/FirePHPCore/lib/FirePHPCore/';
        $chromephp_path = './' . drupal_get_path('module', 'devel') .'/chromephp';
      }

      // include FirePHP if exists...
      if (file_exists($firephp_path .'fb.php')) {
        include_once $firephp_path .'fb.php';
        include_once $firephp_path .'FirePHP.class.php';
      }

      // include ChromePHP if exists...
      if (file_exists($chromephp_path . '/ChromePhp.php')) {
        include_once $chromephp_path . '/ChromePhp.php';
      }


      // Add CSS for query log if should be displayed.
      if (variable_get('devel_query_display', 0)) {
        drupal_add_css(drupal_get_path('module', 'devel') .'/devel.css');
        drupal_add_js(drupal_get_path('module', 'devel'). '/devel.js');
      }
    }
  }
  if (variable_get('devel_rebuild_theme_registry', FALSE)) {
    drupal_theme_rebuild();
    if (flood_is_allowed('devel_rebuild_registry_warning', 1)) {
      flood_register_event('devel_rebuild_registry_warning');
      if (!devel_silent() && user_access('access devel information')) {
        drupal_set_message(t('The theme registry is being rebuilt on every request. Remember to <a href="!url">turn off</a> this feature on production websites.', array("!url" => url('admin/config/development/devel'))));
      }
    }
  }
}

function devel_set_message($msg, $type = NULL) {
  $function  = function_exists('drush_log') ? 'drush_log' : 'drupal_set_message';
  $function($msg, $type);
}

// Return boolean. No need for cache here.
function has_krumo() {
  // see README.txt or just download from http://krumo.sourceforge.net/
  @include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'devel') .'/krumo/class.krumo.php';
  return function_exists('krumo') && !drupal_is_cli();
}

/**
 * Decide whether or not to print a debug variable using krumo().
 *
 * @param $input
 * @return boolean
 */
function merits_krumo($input) {
  return (is_object($input) || is_array($input)) && has_krumo() && variable_get('devel_krumo_skin', '') != 'disabled';
}

/**
 * Calls the http://www.firephp.org/ fb() function if it is found.
 *
 * @return void
 */
function dfb() {
  if (function_exists('fb') && user_access('access devel information') && !headers_sent()) {
    $args = func_get_args();
    call_user_func_array('fb', $args);
  }
}

/**
 * Calls dfb() to output a backtrace.
 */
function dfbt($label) {
  dfb($label, FirePHP::TRACE);
}

/**
 * Wrapper for ChromePHP Class log method
 */
function dcp() {
  if (class_exists('ChromePhp') && user_access('access devel information')) {
    $args = func_get_args();
    call_user_func_array(array('ChromePhp', 'log'), $args);
  }
}

/**
 * Implements hook_watchdog().
 */
function devel_watchdog(array $log_entry) {
  if (class_exists('FirePHP') && !drupal_is_cli()) {
    switch ($log_entry['severity']) {
      case WATCHDOG_EMERGENCY:
      case WATCHDOG_ALERT:
      case WATCHDOG_CRITICAL:
      case WATCHDOG_ERROR:
        $type = FirePHP::ERROR;
        break;
      case WATCHDOG_WARNING:
        $type = FirePHP::WARN;
        break;
      case WATCHDOG_NOTICE:
      case WATCHDOG_INFO:
        $type = FirePHP::INFO;
        break;
      case WATCHDOG_DEBUG:
      DEFAULT:
        $type = FirePHP::LOG;
    }
  }
  else {
    $type = 'watchdog';
  }
  $function = function_exists('decode_entities') ? 'decode_entities' : 'html_entity_decode';
  $watchdog = array(
    'type' => $log_entry['type'],
    'message' => $function(strtr($log_entry['message'], (array)$log_entry['variables'])),
  );
  if (isset($log_entry['link'])) {
    $watchdog['link'] = $log_entry['link'];
  }
  dfb($watchdog, $type);
}

function devel_set_handler($handler) {
  switch ($handler) {
    case DEVEL_ERROR_HANDLER_STANDARD:
      // do nothing
      break;
    case DEVEL_ERROR_HANDLER_BACKTRACE:
      if (has_krumo()) {
        set_error_handler('backtrace_error_handler');
      }
      break;
    case DEVEL_ERROR_HANDLER_NONE:
      restore_error_handler();
      break;
  }
}

function devel_silent() {
  // isset($_GET['q']) is needed when calling the front page. q is not set.
  // Don't interfere with private files/images.
  return
    function_exists('drupal_is_cli') && drupal_is_cli() ||
    (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'ApacheBench') !== FALSE) ||
    !empty($_REQUEST['XDEBUG_PROFILE']) ||
    isset($GLOBALS['devel_shutdown']) ||
    strstr($_SERVER['PHP_SELF'], 'update.php') ||
    (isset($_GET['q']) && (
      in_array($_GET['q'], array( 'admin/content/node-settings/rebuild')) ||
      substr($_GET['q'], 0, strlen('system/files')) == 'system/files' ||
      substr($_GET['q'], 0, strlen('batch')) == 'batch' ||
      substr($_GET['q'], 0, strlen('file/ajax')) == 'file/ajax')
    );
}

function devel_xhprof_enable() {
  if (devel_xhprof_is_enabled()) {
    if ($path = variable_get('devel_xhprof_directory', '')) {
      include_once $path . '/xhprof_lib/utils/xhprof_lib.php';
      include_once $path . '/xhprof_lib/utils/xhprof_runs.php';
      // @todo: consider a variable per-flag instead.
      xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
    }
  }
}

function devel_xhprof_is_enabled() {
  return extension_loaded('xhprof') && variable_get('devel_xhprof_enabled', FALSE);
}

/**
 * Implementation of hook_boot(). Runs even for cached pages.
 */
function devel_boot() {
  // Initialize XHProf.
  devel_xhprof_enable();

  if (!devel_silent()) {
    if (variable_get('dev_mem', 0)) {
      global $memory_init;
      $memory_init = memory_get_usage();
    }

    if (devel_query_enabled()) {
      @include_once DRUPAL_ROOT . '/includes/database/log.inc';
      Database::startLog('devel');;
    }
  }

  // We need user_access() in the shutdown function. make sure it gets loaded.
  // Also prime the drupal_get_filename() static with user.module's location to
  // avoid a stray query.
  drupal_get_filename('module', 'user', 'modules/user/user.module');
  drupal_load('module', 'user');
  drupal_register_shutdown_function('devel_shutdown');
}

function backtrace_error_handler($error_level, $message, $filename, $line, $context) {
  // Hide stack trace and parameters from unqualified users.
  if (!user_access('access devel information')) {
    return _drupal_error_handler($error_level, $message, $filename, $line, $context);
  }
  // Don't respond to the error if it was suppressed with a '@'
  if (error_reporting() == 0) {
    return;
  }
  // Don't respond to warning caused by ourselves.
  if (preg_match('#Cannot modify header information - headers already sent by \\([^\\)]*[/\\\\]devel[/\\\\]#', $message)) {
    return;
  }
  if ($error_level & error_reporting()) {
    // Only write each distinct NOTICE message once, as repeats do not give any
    // further information and can choke the page output.
    if ($error_level == E_NOTICE) {
      static $written = array();
      if (!empty($written[$line][$filename][$message])) {
        return;
      }
      $written[$line][$filename][$message] = TRUE;
    }

    require_once DRUPAL_ROOT . '/includes/errors.inc';
    $types = drupal_error_levels();
    $type = $types[$error_level];
    $backtrace = debug_backtrace();
    array_shift($backtrace);
    $variables = array('%error' => $type[0], '%message' => $message, '%function' => $backtrace[0]['function'] .'()', '%file' => $filename, '%line' => $line);
    $counter = 0;

    if (variable_get('error_level', 1) == 1) {
      foreach ($backtrace as $call) {
        $nicetrace[$call['function'] . '<span class="' . $counter++ . '" />'] = $call;
      }
      print t('%error: %message in %function (line %line of %file).', $variables) ." =&gt;\n";
      krumo($nicetrace);
    }

    watchdog('php', '%error: %message in %function (line %line of %file).', $variables, $type[1]);
  }
}

/**
 * Implement hook_permission().
 */
function devel_permission() {
  return array(
    'access devel information' => array(
      'description' => t('View developer output like variable printouts, query log, etc.'),
      'title' => t('Access developer information'),
      'restrict access' => TRUE,
    ),
    'execute php code' => array(
      'title' => t('Execute PHP code'),
      'description' => t('Run arbitrary PHP from a block.'),
      'restrict access' => TRUE,
    ),
    'switch users' => array(
      'title' => t('Switch users'),
      'description' => t('Become any user on the site with just a click.'),
      'restrict access' => TRUE,
    ),
    'display source code' => array(
      'title' => t('Display source code'),
      'description' => t('View the site\'s php source code.'),
      'restrict access' => TRUE,
    ),
  );
}

function devel_block_info() {
  $blocks['execute_php'] = array(
    'info' => t('Execute PHP'),
    'cache' => DRUPAL_NO_CACHE,
  );
  $blocks['switch_user'] = array(
    'info' => t('Switch user'),
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implementation of hook_block_configure().
 */
function devel_block_configure($delta) {
  if ($delta == 'switch_user') {
    $form['list_size'] = array(
      '#type' => 'textfield',
      '#title' => t('Number of users to display in the list'),
      '#default_value' => variable_get('devel_switch_user_list_size', 10),
      '#size' => '3',
      '#maxlength' => '4',
    );
    $form['include_anon'] = array(
      '#type' => 'checkbox',
      '#title' => t('Include %anonymous', array('%anonymous' => format_username(drupal_anonymous_user()))),
      '#default_value' => variable_get('devel_switch_user_include_anon', FALSE),
    );
    $form['show_form'] = array(
      '#type' => 'checkbox',
      '#title' => t('Allow entering any user name'),
      '#default_value' => variable_get('devel_switch_user_show_form', TRUE),
    );
    return $form;
  }
}

function devel_block_save($delta, $edit = array()) {
  if ($delta == 'switch_user') {
    variable_set('devel_switch_user_list_size', $edit['list_size']);
    variable_set('devel_switch_user_include_anon', $edit['include_anon']);
    variable_set('devel_switch_user_show_form', $edit['show_form']);
  }
}

function devel_block_view($delta) {
  $block = array();
  switch ($delta) {
    case 'switch_user':
      $block = devel_block_switch_user();
      break;

    case 'execute_php':
      if (user_access('execute php code')) {
        $block['content'] = drupal_get_form('devel_execute_block_form');
      }
      break;
  }
  return $block;
}

function devel_block_switch_user() {
  $links = devel_switch_user_list();
  if (!empty($links) || user_access('switch users')) {
    $block['subject'] = t('Switch user');
    $build['devel_links'] = array('#theme' => 'links', '#links' => $links);
    if (variable_get('devel_switch_user_show_form', TRUE)) {
      $build['devel_form'] = drupal_get_form('devel_switch_user_form');
    }
    $block['content'] = $build;
    return $block;
  }
}

function devel_switch_user_list() {
  global $user;

  $links = array();
  if (user_access('switch users')) {
    $list_size = variable_get('devel_switch_user_list_size', 10);
    if ($include_anon = ($user->uid && variable_get('devel_switch_user_include_anon', FALSE))) {
      --$list_size;
    }
    $dest = drupal_get_destination();
    // Try to find at least $list_size users that can switch.
    // Inactive users are omitted from all of the following db selects.
    $roles = user_roles(TRUE, 'switch users');
    $query = db_select('users', 'u');
    $query->addField('u', 'uid');
    $query->addField('u', 'access');
    $query->distinct();
    $query->condition('u.uid', 0, '>');
    $query->condition('u.status', 0, '>');
    $query->orderBy('u.access', 'DESC');
    $query->range(0, $list_size);

    if (!isset($roles[DRUPAL_AUTHENTICATED_RID])) {
      $query->leftJoin('users_roles', 'r', 'u.uid = r.uid');
      $or_condition = db_or();
      $or_condition->condition('u.uid', 1);
      if (!empty($roles)) {
        $or_condition->condition('r.rid', array_keys($roles), 'IN');
      }
      $query->condition($or_condition);
    }

    $uids = $query->execute()->fetchCol();
    $accounts = user_load_multiple($uids);

    foreach ($accounts as $account) {
      $links[$account->uid] = array(
        'title' => drupal_placeholder(format_username($account)),
        'href' => 'devel/switch/'. $account->name,
        'query' => $dest,
        'attributes' => array('title' => t('This user can switch back.')),
        'html' => TRUE,
        'last_access' => $account->access,
      );
    }
    $num_links = count($links);
    if ($num_links < $list_size) {
      // If we don't have enough, add distinct uids until we hit $list_size.
      $uids = db_query_range('SELECT uid FROM {users} WHERE uid > 0 AND uid NOT IN (:uids) AND status > 0 ORDER BY access DESC', 0, $list_size - $num_links, array(':uids' => array_keys($links)))->fetchCol();
      $accounts = user_load_multiple($uids);
      foreach ($accounts as $account) {
        $links[$account->uid] = array(
          'title' => format_username($account),
          'href' => 'devel/switch/'. $account->name,
          'query' => $dest,
          'attributes' => array('title' => t('Caution: this user will be unable to switch back.')),
          'last_access' => $account->access,
        );
      }
      uasort($links, '_devel_switch_user_list_cmp');
    }
    if ($include_anon) {
      $link = array(
        'title' => format_username(drupal_anonymous_user()),
        'href' => 'devel/switch',
        'query' => $dest,
        'attributes' => array('title' => t('Caution: the anonymous user will be unable to switch back.')),
      );
      if (user_access('switch users', drupal_anonymous_user())) {
        $link['title'] = drupal_placeholder($link['title']);
        $link['attributes'] = array('title' => t('This user can switch back.'));
        $link['html'] = TRUE;
      }
      $links[] = $link;
    }
  }
  return $links;
}

/**
 * Comparison helper function for uasort() in devel_switch_user_list().
 *
 * Sorts the Switch User links by the user's last access timestamp.
 */
function _devel_switch_user_list_cmp($a, $b) {
  return $b['last_access'] - $a['last_access'];
}

function devel_switch_user_form() {
  $form['username'] = array(
    '#type' => 'textfield',
    '#description' => t('Enter username'),
    '#autocomplete_path' => 'user/autocomplete',
    '#maxlength' => USERNAME_MAX_LENGTH,
    '#size' => 16,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Switch'),
  );
  return $form;

}

function devel_doc_function_form() {
  $version = devel_get_core_version(VERSION);
  $form['function'] = array(
    '#type' => 'textfield',
    '#description' => t('Enter function name for api lookup'),
    '#size' => 16,
    '#maxlength' => 255,
  );
  $form['version'] = array('#type' => 'value', '#value' => $version);
  $form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

function devel_doc_function_form_submit($form, &$form_state) {
  $version = $form_state['values']['version'];
  $function = $form_state['values']['function'];
  $api = variable_get('devel_api_url', 'api.drupal.org');
  $form_state['redirect'] =   "http://$api/api/function/$function/$version";
}

function devel_switch_user_form_validate($form, &$form_state) {
  if (!$account = user_load_by_name($form_state['values']['username'])) {
    form_set_error('username', t('Username not found'));
  }
}

function devel_switch_user_form_submit($form, &$form_state) {
  $form_state['redirect'] = 'devel/switch/'. $form_state['values']['username'];
}

/**
 * Implements hook_drupal_goto_alter().
 */
function devel_drupal_goto_alter($path, $options, $http_response_code) {
  global $user;

  if (isset($path) && !devel_silent()) {
    // The page we are leaving is a drupal_goto(). Present a redirection page
    // so that the developer can see the intermediate query log.
    // We don't want to load user module here, so keep function_exists() call.
    if (isset($user) && function_exists('user_access') && user_access('access devel information') && variable_get('devel_redirect_page', 0)) {
      $destination = function_exists('url') ? url($path, $options) : $path;
      $output = t_safe('<p>The user is being redirected to <a href="@destination">@destination</a>.</p>', array('@destination' => $destination));
      drupal_deliver_page($output);

      // Don't allow the automatic redirect to happen.
      exit();
    }
    else {
      $GLOBALS['devel_redirecting'] = TRUE;
    }
  }
}

/**
 * Implements hook_library_alter().
 */
function devel_library_alter(&$libraries, $module) {
  // Use an uncompressed version of jQuery for debugging.
  if ($module === 'system' && variable_get('devel_use_uncompressed_jquery', FALSE) && isset($libraries['jquery'])) {
    // Make sure we're not changing the jQuery version used on the site.
    if (version_compare($libraries['jquery']['version'], '1.4.4', '=')) {
      $libraries['jquery']['js'] = array(
        drupal_get_path('module', 'devel') . '/jquery-1.4.4-uncompressed.js' => array('weight' => JS_LIBRARY - 20),
      );
    }
    else {
      if (!devel_silent() && user_access('access devel information')) {
        drupal_set_message(t('jQuery could not be replaced with an uncompressed version of 1.4.4, because jQuery @version is running on the site.', array('@version' => $libraries['jquery']['version'])));
      }

    }
  }
}

/**
 * See devel_start() which registers this function as a shutdown function.
 */
function devel_shutdown() {
  // Register the real shutdown function so it runs later than other shutdown functions.
  drupal_register_shutdown_function('devel_shutdown_real');

  global $devel_run_id;
  $devel_run_id = devel_xhprof_is_enabled() ? devel_shutdown_xhprof(): NULL;
  if ($devel_run_id && function_exists('drush_log')) {
    drush_log('xhprof link: ' . devel_xhprof_link($devel_run_id, 'url'), 'notice');
  }
}

function devel_page_alter($page) {
  if (variable_get('devel_page_alter', FALSE) && user_access('access devel information')) {
    dpm($page, 'page');
  }
}

// AJAX render reponses sometimers are sent as text/html so we have to catch them here
// and disable our footer stuff.
function devel_ajax_render_alter() {
  $GLOBALS['devel_shutdown'] = FALSE;
}

/**
 * See devel_shutdown() which registers this function as a shutdown function. Displays developer information in the footer.
 */
function devel_shutdown_real() {
  global $user;
  $output = $txt = '';

  // Set $GLOBALS['devel_shutdown'] = FALSE in order to supress the
  // devel footer for a page.  Not necessary if your page outputs any
  // of the Content-type http headers tested below (e.g. text/xml,
  // text/javascript, etc).  This is is advised where applicable.
  if (!devel_silent() && !isset($GLOBALS['devel_shutdown']) && !isset($GLOBALS['devel_redirecting'])) {
    // Try not to break non html pages.
    if (function_exists('drupal_get_http_header')) {
      $header = drupal_get_http_header('content-type');
      if ($header) {
        $formats = array('xml', 'javascript', 'json', 'plain', 'image', 'application', 'csv', 'x-comma-separated-values');
        foreach ($formats as $format) {
          if (strstr($header, $format)) {
            return;
          }
        }
      }
    }

    if (isset($user) && user_access('access devel information')) {
      $queries = Database::getLog('devel', 'default');
      $output .= devel_shutdown_summary($queries);
      $output .= devel_shutdown_query($queries);
    }

    if ($output) {
      // TODO: gzip this text if we are sending a gzip page. see drupal_page_header().
      // For some reason, this is not actually printing for cached pages even though it gets executed
      // and $output looks good.
      print $output;
    }
  }
}

function devel_shutdown_summary($queries) {
  $sum = 0;
  $output = '';
  list($counts, $query_summary) = devel_query_summary($queries);

  if (variable_get('devel_query_display', FALSE)) {
    // Query log on.
    $output .= $query_summary;
    $output .= t_safe(' Queries exceeding @threshold ms are <span class="marker">highlighted</span>.', array('@threshold' => variable_get('devel_execution', 5)));
  }

  if (variable_get('dev_timer', 0)) {
    $output .= devel_timer();
  }

  if (devel_xhprof_is_enabled()) {
    $output .= ' ' . devel_xhprof_link($GLOBALS['devel_run_id']);
  }

  $output .= devel_shutdown_memory();

  if ($output) {
    return '<div class="dev-query">' . $output . '</div>';
  }
}

function devel_shutdown_xhprof() {
  $namespace = variable_get('site_name', '');  // namespace for your application
  $xhprof_data = xhprof_disable();
  $xhprof_runs = new XHProfRuns_Default();
  return $xhprof_runs->save_run($xhprof_data, $namespace);
}

function devel_xhprof_link($run_id, $type = 'link') {
  // @todo: render results from within Drupal.
  $xhprof_url = variable_get('devel_xhprof_url', '');
  $namespace = variable_get('site_name', '');  // namespace for your application
  if ($xhprof_url) {
    $url  = $xhprof_url . "/index.php?run=$run_id&source=$namespace";
    return $type == 'url' ? $url : t('<a href="@xhprof">XHProf output</a>. ', array('@xhprof' => $url));
  }
}

function devel_shutdown_memory() {
  global $memory_init;

  if (variable_get('dev_mem', FALSE)) {
    $memory_shutdown = memory_get_usage();
    $args = array('@memory_boot' => round($memory_init / 1024 / 1024, 2), '@memory_shutdown' => round($memory_shutdown / 1024 / 1024, 2), '@memory_peak' => round(memory_get_peak_usage(TRUE) / 1024 / 1024, 2));
    $msg = '<span class="dev-memory-usages"> Memory used at: devel_boot()=<strong>@memory_boot</strong> MB, devel_shutdown()=<strong>@memory_shutdown</strong> MB, PHP peak=<strong>@memory_peak</strong> MB.</span>';
    // theme() may not be available. not t() either.
    return t_safe($msg, $args);
  }
}

function devel_shutdown_query($queries) {
  if (!empty($queries)) {
    if (function_exists('theme_get_registry') && theme_get_registry()) {
      // Safe to call theme('table).
      list($counts, $query_summary) = devel_query_summary($queries);
      $output = devel_query_table($queries, $counts);

      // Save all queries to a file in temp dir. Retrieved via AJAX.
      devel_query_put_contents($queries);
    }
    else {
      $output = '</div>' . dprint_r($queries, TRUE);
    }
    return $output;
  }
}

// Write the variables information to the a file. It will be retrieved on demand via AJAX.
function devel_query_put_contents($queries) {
  $request_id = mt_rand(1, 1000000);
  $path = "temporary://devel_querylog";

  // Create the devel_querylog within the temp folder, if needed.
  file_prepare_directory($path, FILE_CREATE_DIRECTORY);

  // Occassionally wipe the querylog dir so that files don't accumulate.
  if (mt_rand(1, 1000) == 401) {
    devel_empty_dir($path);
  }

  $path .= "/$request_id.txt";
  $path = file_stream_wrapper_uri_normalize($path);
  // Save queries as a json array. Suppress errors due to recursion ()
  file_put_contents($path, @json_encode($queries));
  $settings['devel'] = array(
    // A random string that is sent to the browser. It enables the AJAX to retrieve queries from this request.
    'request_id' => $request_id,
  );
  print '<script type="text/javascript">jQuery.extend(Drupal.settings, '.  json_encode($settings) .");</script>\n";
}

function devel_query_enabled() {
  return method_exists('Database', 'getLog') && variable_get('devel_query_display', FALSE);
}

function devel_query_summary($queries) {
  if (variable_get('devel_query_display', FALSE) && is_array($queries)) {
    $sum = 0;
    foreach ($queries as $query) {
      $text[] = $query['query'];
      $sum += $query['time'];
    }
    $counts = array_count_values($text);
    return array($counts, t_safe('Executed @queries queries in @time ms.', array('@queries' => count($queries), '@time' => round($sum * 1000, 2))));
  }
}

function t_safe($string, $args) {
  // get_t caused problems here with theme registry after changing on admin/build/modules. the theme_get_registry call is needed.
  if (function_exists('t') && function_exists('theme_get_registry')) {
    theme_get_registry();
    return t($string, $args);
  }
  else {
    strtr($string, $args);
  }
}

function devel_get_core_version($version) {
  $version_parts = explode('.', $version);
  // Map from 4.7.10 -> 4.7
  if ($version_parts[0] < 5) {
    return $version_parts[0] .'.'. $version_parts[1];
  }
  // Map from 5.5 -> 5 or 6.0-beta2 -> 6
  else {
    return $version_parts[0];
  }
}

// See http://drupal.org/node/126098
function devel_is_compatible_optimizer() {
   ob_start();
   phpinfo();
   $info = ob_get_contents();
   ob_end_clean();

   // Match the Zend Optimizer version in the phpinfo information
   $found = preg_match('/Zend&nbsp;Optimizer&nbsp;v([0-9])\.([0-9])\.([0-9])/', $info, $matches);

   if ($matches) {
     $major = $matches[1];
     $minor = $matches[2];
     $build = $matches[3];

     if ($major >= 3) {
       if ($minor >= 3) {
         return TRUE;
       }
       elseif ($minor == 2 && $build >= 8) {
         return TRUE;
       }
       else {
         return FALSE;
       }
    }
    else {
      return FALSE;
    }
  }
  else {
    return TRUE;
  }
}

/**
 * Generates the execute block form.
 */
function devel_execute_block_form() {
  $form['execute'] = array(
    '#type' => 'fieldset',
    '#title' => t('Execute PHP Code'),
    '#collapsible' => TRUE,
    '#collapsed' => (!isset($_SESSION['devel_execute_code'])),
  );
  $form['#submit'] = array('devel_execute_form_submit');
  return array_merge_recursive($form, devel_execute_form());
}

/**
 * Generates the execute form.
 */
function devel_execute_form() {
  $form['execute']['code'] = array(
    '#type' => 'textarea',
    '#title' => t('PHP code to execute'),
    '#description' => t('Enter some code. Do not use <code>&lt;?php ?&gt;</code> tags.'),
    '#default_value' => (isset($_SESSION['devel_execute_code']) ? $_SESSION['devel_execute_code'] : ''),
    '#rows' => 20,
  );
  $form['execute']['op'] = array('#type' => 'submit', '#value' => t('Execute'));
  $form['#redirect'] = FALSE;
  if (isset($_SESSION['devel_execute_code'])) {
    unset($_SESSION['devel_execute_code']);
  }
  return $form;
}

/**
 * Process PHP execute form submissions.
 */
function devel_execute_form_submit($form, &$form_state) {
  ob_start();
  print eval($form_state['values']['code']);
  $_SESSION['devel_execute_code'] = $form_state['values']['code'];
  dsm(ob_get_clean());
}

/**
 * Switch from original user to another user and back.
 * We don't call session_save_session() because we really want to change users. Usually unsafe!
 *
 * @param $name The username to switch to, or NULL to log out.
 */
function devel_switch_user($name = NULL) {
  global $user;

  if ($user->uid) {
    module_invoke_all('user_logout', $user);
  }
  if (isset($name) && $account = user_load_by_name($name)) {
    $old_uid = $user->uid;
    $user = $account;
    $user->timestamp = time() - 9999;
    if (!$old_uid) {
      // Switch from anonymous to authorized.
      drupal_session_regenerate();
    }
    $edit = array();
    user_module_invoke('login', $edit, $user);
  }
  elseif ($user->uid) {
    session_destroy();
  }
  drupal_goto();
}

/**
 * Print an object or array using either Krumo (if installed) or devel_print_object()
 *
 * @param $object
 *   array or object to print
 * @param $prefix
 *   prefixing for output items
 */
function kdevel_print_object($object, $prefix = NULL) {
  return has_krumo() ? krumo_ob($object) : devel_print_object($object, $prefix);
}

// Save krumo htlm using output buffering.
function krumo_ob($object) {
  ob_start();
  krumo($object);
  $output = ob_get_contents();
  ob_end_clean();
  return $output;
}

/**
 * Display an object or array
 *
 * @param $object
 *   the object or array
 * @param $prefix
 *   prefix for the output items (example "$node->", "$user->", "$")
 * @param $header
 *   set to FALSE to suppress the output of the h3
 */
function devel_print_object($object, $prefix = NULL, $header = TRUE) {
  drupal_add_css(drupal_get_path('module', 'devel') .'/devel.css');
  $output = '<div class="devel-obj-output">';
  if ($header) {
    $output .= '<h3>'. t('Display of !type !obj', array('!type' => str_replace(array('$', '->'), '', $prefix), '!obj' => gettype($object))) .'</h3>';
  }
  $output .= _devel_print_object($object, $prefix);
  $output .= '</div>';
  return $output;
}

/**
 * Recursive (and therefore magical) function goes through an array or object and
 * returns a nicely formatted listing of its contents.
 *
 * @param $obj
 *   array or object to recurse through
 * @param $prefix
 *   prefix for the output items (example "$node->", "$user->", "$")
 * @param $parents
 *   used by recursion
 * @param $object
 *   used by recursion
 * @return
 *   fomatted html
 *
 * @todo
 *   currently there are problems sending an array with a varname
 */
function _devel_print_object($obj, $prefix = NULL, $parents = NULL, $object = FALSE) {
  static $root_type, $out_format;

  // TODO: support objects with references. See http://drupal.org/node/234581.
  if (isset($obj->view)) {
    return;
  }

  if (!isset($root_type)) {
    $root_type = gettype($obj);
    if ($root_type == 'object') {
      $object = TRUE;
    }
  }

  if (is_object($obj)) {
    $obj = (array)$obj;
  }
  if (is_array($obj)) {
    $output = "<dl>\n";
    foreach ($obj as $field => $value) {
      if ($field == 'devel_flag_reference') {
        continue;
      }
      if (!is_null($parents)) {
        if ($object) {
          $field = $parents .'->'. $field;
        }
        else {
          if (is_int($field)) {
            $field = $parents .'['. $field .']';
          }
          else {
            $field = $parents .'[\''. $field .'\']';
          }
        }
      }

      $type = gettype($value);

      $show_summary = TRUE;
      $summary = NULL;
      if ($show_summary) {
        switch ($type) {
          case 'string' :
          case 'float' :
          case 'integer' :
            if (strlen($value) == 0) {
              $summary = t("{empty}");
            }
            elseif (strlen($value) < 40) {
              $summary = htmlspecialchars($value);
            }
            else {
              $summary = format_plural(drupal_strlen($value), '1 character', '@count characters');
            }
            break;
          case 'array' :
          case 'object' :
            $summary = format_plural(count((array)$value), '1 element', '@count elements');
            break;
          case 'boolean' :
            $summary = $value ? t('TRUE') : t('FALSE');
            break;
        }
      }
      if (!is_null($summary)) {
        $typesum = '('. $type .', <em>'. $summary .'</em>)';
      }
      else {
        $typesum = '('. $type .')';
      }

      $output .= '<span class="devel-attr">';
      $output .= "<dt><span class=\"field\">{$prefix}{$field}</span> $typesum</dt>\n";
      $output .= "<dd>\n";
      // Check for references.
      if (is_array($value) && isset($value['devel_flag_reference'])) {
        $value['devel_flag_reference'] = TRUE;
      }
      // Check for references to prevent errors from recursions.
      if (is_array($value) && isset($value['devel_flag_reference']) && !$value['devel_flag_reference']) {
        $value['devel_flag_reference'] = FALSE;
        $output .= _devel_print_object($value, $prefix, $field);
      }
      elseif (is_object($value)) {
        $value->devel_flag_reference = FALSE;
        $output .= _devel_print_object((array)$value, $prefix, $field, TRUE);
      }
      else {
        $value = is_bool($value) ? ($value ? 'TRUE' : 'FALSE') : $value;
        $output .= htmlspecialchars(print_r($value, TRUE)) ."\n";
      }
      $output .= "</dd></span>\n";
    }
    $output .= "</dl>\n";
  }
  return $output;
}

/**
 * Adds a table at the bottom of the page cataloguing data on all the database queries that were made to
 * generate the page.
 */
function devel_query_table($queries, $counts) {
  $version = devel_get_core_version(VERSION);
  $header = array ('ms', '#', 'where', 'ops', 'query', 'target');
  $i = 0;
  $api = variable_get('devel_api_url', 'api.drupal.org');
  foreach ($queries as $query) {
    $function = !empty($query['caller']['class']) ? $query['caller']['class'] . '::' : '';
    $function .= $query['caller']['function'];
    $count = isset($counts[$query['query']]) ? $counts[$query['query']] : 0;

    $diff = round($query['time'] * 1000, 2);
    if ($diff > variable_get('devel_execution', 5)) {
      $cell[$i][] = array ('data' => $diff, 'class' => 'marker');
    }
    else {
      $cell[$i][] = $diff;
    }
    $cell[$i][] = $count;
    $cell[$i][] = l($function, "http://$api/api/function/$function/$version");
    $ops[] = l('P', '', array('attributes' => array('title' => 'Show placeholders', 'class' => 'dev-placeholders', 'qid' => $i)));
    $ops[] = l('A', '', array('attributes' => array('title' => 'Show arguments', 'class' => 'dev-arguments', 'qid' => $i)));
    // EXPLAIN only valid for select queries.
    if (strpos($query['query'], 'UPDATE') === FALSE && strpos($query['query'], 'INSERT') === FALSE && strpos($query['query'], 'DELETE') === FALSE) {
      $ops[] = l('E', '', array('attributes' => array('title' => 'Show EXPLAIN', 'class' => 'dev-explain', 'qid' => $i)));
    }
    $cell[$i][] = implode(' ', $ops);
    // 3 divs for each variation of the query. Last 2 are hidden by default.
    $placeholders = '<div class="dev-placeholders">' . check_plain($query['query']) . "</div>\n";
    $args = '<div class="dev-arguments" style="display: none;"></div>' . "\n";
    $explain = '<div class="dev-explain" style="display: none;"></div>' . "\n";
    $cell[$i][] = array(
      'id' => "devel-query-$i",
      'data' => $placeholders . $args . $explain,
    );
    $cell[$i][] = $query['target'];
    $i++;
    unset($diff, $count, $ops);
  }
  if (variable_get('devel_query_sort', DEVEL_QUERY_SORT_BY_SOURCE)) {
    usort($cell, '_devel_table_sort');
  }
  return theme('devel_querylog', array('header' => $header, 'rows' => $cell));
}

function theme_devel_querylog_row($variables) {
  $row = $variables['row'];
  $i = 0;
  $output = '';
  foreach ($row as $cell) {
    $i++;

    if (is_array($cell)) {
      $data = !empty($cell['data']) ? $cell['data'] : '';
      unset($cell['data']);
      $attr = $cell;
    }
    else {
      $data = $cell;
      $attr = array();
    }

    if (!empty($attr['class'])) {
      $attr['class'] .= " cell cell-$i";
    }
    else {
      $attr['class'] = "cell cell-$i";
    }
    $attr = drupal_attributes($attr);

    $output .= "<div $attr>$data</div>";
  }
  return $output;
}

function theme_devel_querylog($variables) {
  $header = $variables['header'];
  $rows = $variables['rows'];
  $output = '';
  if (!empty($header)) {
    $output .= "<div class='devel-querylog devel-querylog-header clear-block'>";
    $output .= theme('devel_querylog_row', array('row' => $header));
    $output .= "</div>";
  }
  if (!empty($rows)) {
    $i = 0;
    foreach ($rows as $row) {
      $i++;
      $zebra = ($i % 2) == 0 ? 'even' : 'odd';
      $output .= "<div class='devel-querylog devel-querylog-$zebra clear-block'>";
      $output .= theme('devel_querylog_row', array('row' => $row));
      $output .= "</div>";
    }
  }
  return $output;
}

function _devel_table_sort($a, $b) {
  $a = is_array($a[0]) ? $a[0]['data'] : $a[0];
  $b = is_array($b[0]) ? $b[0]['data'] : $b[0];
  if ($a < $b) {
    return 1;
  }
  if ($a > $b) {
    return -1;
  }
  return 0;
}

/**
 * Displays page execution time at the bottom of the page.
 */
function devel_timer() {
  $time = timer_read('page');
  return t_safe(' Page execution time was @time ms.', array('@time' => $time));
}

// An alias for drupal_debug().
function dd($data, $label = NULL) {
  return drupal_debug($data, $label);
}

// Log any variable to a drupal_debug.log in the site's temp directory.
// See http://drupal.org/node/314112
function drupal_debug($data, $label = NULL) {
  ob_start();
  print_r($data);
  $string = ob_get_clean();
  if ($label) {
    $out = $label .': '. $string;
  }
  else {
    $out = $string;
  }
  $out .= "\n";

  // The temp directory does vary across multiple simpletest instances.
  $file = 'temporary://drupal_debug.txt';
  if (file_put_contents($file, $out, FILE_APPEND) === FALSE) {
    drupal_set_message(t('The file could not be written.'), 'error');
    return FALSE;
  }
}

/**
 * Prints the arguments passed into the current function
 */
function dargs($always = TRUE) {
  static $printed;
  if ($always || !$printed) {
    $bt = debug_backtrace();
    print kdevel_print_object($bt[1]['args']);
    $printed = TRUE;
  }
}

/**
 * Print a SQL string from a DBTNG Query object. Includes quoted arguments.
 *
 * @param $query
 *   A Query object.
 * @param $return
 *   Whether to return or print the string. Default to FALSE.
 * @param $name
 *   Optional name for identifying the output.
 */
function dpq($query, $return = FALSE, $name = NULL) {
  if (user_access('access devel information')) {
    $query->preExecute();
    $sql = (string) $query;
    $quoted = array();
    $connection = Database::getConnection();
    foreach ((array)$query->arguments() as $key => $val) {
      $quoted[$key] = $connection->quote($val);
    }
    $sql = strtr($sql, $quoted);
    if ($return) {
      return $sql;
    }
    else {
      dpm($sql, $name);
    }
  }
}

/**
 * Print a variable to the 'message' area of the page. Uses drupal_set_message()
 */
function dpm($input, $name = NULL) {
  if (user_access('access devel information')) {
    $export = kprint_r($input, TRUE, $name);
    drupal_set_message($export);
  }
}

/**
 * drupal_var_export() a variable to the 'message' area of the page. Uses drupal_set_message()
 */
function dvm($input, $name = NULL) {
  if (user_access('access devel information')) {
    $export = dprint_r($input, TRUE, $name, 'drupal_var_export', FALSE);
    drupal_set_message($export);
  }
}

// legacy function that was poorly named. use dpm() instead, since the 'p' maps to 'print_r'
function dsm($input, $name = NULL) {
  dpm($input, $name);
}

/**
 * An alias for dprint_r(). Saves carpal tunnel syndrome.
 */
function dpr($input, $return = FALSE, $name = NULL) {
  return dprint_r($input, $return, $name);
}

/**
 * An alias for kprint_r(). Saves carpal tunnel syndrome.
 */
function kpr($input, $return = FALSE, $name = NULL) {
  return kprint_r($input, $return, $name);
}

/**
 * Like dpr, but uses drupal_var_export() instead
 */
function dvr($input, $return = FALSE, $name = NULL) {
  return dprint_r($input, $return, $name, 'drupal_var_export', FALSE);
}

function kprint_r($input, $return = FALSE, $name = NULL, $function = 'print_r') {
  // We do not want to krumo() strings and integers and such
  if (merits_krumo($input)) {
    if (user_access('access devel information')) {
      return $return ? (isset($name) ? $name .' => ' : '') . krumo_ob($input) : krumo($input);
    }
  }
  else {
    return dprint_r($input, $return, $name, $function);
  }
}

/**
 * Pretty-print a variable to the browser (no krumo).
 * Displays only for users with proper permissions. If
 * you want a string returned instead of a print, use the 2nd param.
 */
function dprint_r($input, $return = FALSE, $name = NULL, $function = 'print_r', $check= TRUE) {
  if (user_access('access devel information')) {
    if ($name) {
      $name .= ' => ';
    }
    if ($function == 'drupal_var_export') {
      include_once DRUPAL_ROOT . '/includes/utility.inc';
      $output = drupal_var_export($input);
    }
    else {
      ob_start();
      $function($input);
      $output = ob_get_clean();
    }

    if ($check) {
      $output = check_plain($output);
    }
    if (count($input, COUNT_RECURSIVE) > DEVEL_MIN_TEXTAREA) {
        // don't use fapi here because sometimes fapi will not be loaded
        $printed_value = "<textarea rows=30 style=\"width: 100%;\">\n". $name . $output .'</textarea>';
    }
    else {
      $printed_value = '<pre>'. $name . $output .'</pre>';
    }

    if ($return) {
      return $printed_value;
    }
    else {
      print $printed_value;
    }
  }
}

/**
 * Prints a renderable array element to the screen using kprint_r().
 *
 * #pre_render and/or #post_render pass-through callback for kprint_r().
 *
 * @todo Investigate appending to #suffix.
 * @todo Investigate label derived from #id, #title, #name, and #theme.
 */
function devel_render() {
  $args = func_get_args();
  // #pre_render and #post_render pass the rendered $element as last argument.
  kprint_r(end($args));
  // #pre_render and #post_render expect the first argument to be returned.
  return reset($args);
}

/**
 * Print the function call stack.
 */
function ddebug_backtrace() {
  if (user_access('access devel information')) {
    $trace = debug_backtrace();
    array_shift($trace);
    $count = count($trace);
    foreach ($trace as $i => $call) {
      $key = ($count - $i) . ': ' . $call['function'];
      $rich_trace[$key] = $call;
    }
    if (has_krumo()) {
      print krumo($rich_trace);
    }
    else {
      dprint_r($rich_trace);
    }
  }
}

// Delete all files in a dir. http://www.plus2net.com/php_tutorial/php-files-delete.php
function devel_empty_dir($dir) {
  foreach (new DirectoryIterator($dir) as $fileInfo) {
    unlink($fileInfo->getPathname());
  }
}

/*
 * migration related functions
 */

/**
 * Regenerate the data in node_comment_statistics table. Technique comes from
 * http://www.artfulsoftware.com/infotree/queries.php?&bw=1280#101
 *
 * @return void
 **/
function devel_rebuild_node_comment_statistics() {
  // Empty table
  db_truncate('node_comment_statistics')->execute();

  // TODO: DBTNG. Ignore keyword is Mysql only? Is only used in the rare case when
  // two comments on the same node share same timestamp.
  $sql = "
    INSERT IGNORE INTO {node_comment_statistics} (nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) (
      SELECT c.nid, c.cid, c.created, c.name, c.uid, c2.comment_count FROM {comment} c
      JOIN (
        SELECT c.nid, MAX(c.created) AS created, COUNT(*) AS comment_count FROM {comment} c WHERE status = 1 GROUP BY c.nid
      ) AS c2 ON c.nid = c2.nid AND c.created = c2.created
    )";
  db_query($sql, array(':published' => COMMENT_PUBLISHED));

  // Insert records into the node_comment_statistics for nodes that are missing.
  $query = db_select('node', 'n');
  $query->leftJoin('node_comment_statistics', 'ncs', 'ncs.nid = n.nid');
  $query->addField('n', 'changed', 'last_comment_timestamp');
  $query->addField('n', 'uid', 'last_comment_uid');
  $query->addField('n', 'nid');
  $query->addExpression('0', 'comment_count');
  $query->addExpression('NULL', 'last_comment_name');
  $query->isNull('ncs.comment_count');

  db_insert('node_comment_statistics')
    ->from($query)
    ->execute();
}