| 1 | <?php | 
|---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- | 
|---|
| 3 | # | 
|---|
| 4 | # This file is part of Dotclear 2. | 
|---|
| 5 | # | 
|---|
| 6 | # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear | 
|---|
| 7 | # Licensed under the GPL version 2.0 license. | 
|---|
| 8 | # See LICENSE file or | 
|---|
| 9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html | 
|---|
| 10 | # | 
|---|
| 11 | # -- END LICENSE BLOCK ----------------------------------------- | 
|---|
| 12 | if (!defined('DC_RC_PATH')) { return; } | 
|---|
| 13 |  | 
|---|
| 14 | include dirname(__FILE__).'/_default_widgets.php'; | 
|---|
| 15 | require_once dirname(__FILE__).'/_widgets_functions.php'; | 
|---|
| 16 |  | 
|---|
| 17 | $core->tpl->addValue('Widgets',array('publicWidgets','tplWidgets')); | 
|---|
| 18 | $core->tpl->addBlock('Widget',array('publicWidgets','tplWidget')); | 
|---|
| 19 |  | 
|---|
| 20 | class publicWidgets | 
|---|
| 21 | { | 
|---|
| 22 | public static function tplWidgets($attr) | 
|---|
| 23 | { | 
|---|
| 24 | $type = isset($attr['type']) ? $attr['type'] : 'nav'; | 
|---|
| 25 |  | 
|---|
| 26 | # widgets to disable | 
|---|
| 27 | $disable = isset($attr['disable']) ? trim($attr['disable']) : ''; | 
|---|
| 28 |  | 
|---|
| 29 | return | 
|---|
| 30 | '<?php '. | 
|---|
| 31 | "publicWidgets::widgetsHandler('".addslashes($type)."','".addslashes($disable)."'); ". | 
|---|
| 32 | ' ?>'; | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | public static function widgetsHandler($type,$disable='') | 
|---|
| 36 | { | 
|---|
| 37 | $wtype = 'widgets_'.$type; | 
|---|
| 38 | $GLOBALS['core']->blog->settings->addNameSpace('widgets'); | 
|---|
| 39 | $widgets = $GLOBALS['core']->blog->settings->widgets->{$wtype}; | 
|---|
| 40 |  | 
|---|
| 41 | if (!$widgets) { // If widgets value is empty, get defaults | 
|---|
| 42 | $widgets = self::defaultWidgets($type); | 
|---|
| 43 | } else { // Otherwise, load widgets | 
|---|
| 44 | $widgets = dcWidgets::load($widgets); | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | if ($widgets->isEmpty()) { // Widgets are empty, don't show anything | 
|---|
| 48 | return; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | $disable = preg_split('/\s*,\s*/',$disable,-1,PREG_SPLIT_NO_EMPTY); | 
|---|
| 52 | $disable = array_flip($disable); | 
|---|
| 53 |  | 
|---|
| 54 | foreach ($widgets->elements() as $k => $w) | 
|---|
| 55 | { | 
|---|
| 56 | if (isset($disable[$w->id()])) { | 
|---|
| 57 | continue; | 
|---|
| 58 | } | 
|---|
| 59 | echo $w->call($k); | 
|---|
| 60 | } | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | private static function defaultWidgets($type) | 
|---|
| 64 | { | 
|---|
| 65 | $widgets = new dcWidgets(); | 
|---|
| 66 | $w = new dcWidgets(); | 
|---|
| 67 |  | 
|---|
| 68 | if (isset($GLOBALS['__default_widgets'][$type])) { | 
|---|
| 69 | $w = $GLOBALS['__default_widgets'][$type]; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | return $w; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | public static function tplWidget($attr,$content) | 
|---|
| 76 | { | 
|---|
| 77 | if (!isset($attr['id']) || !($GLOBALS['__widgets']->{$attr['id']} instanceof dcWidget)) { | 
|---|
| 78 | return; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | # We change tpl:lang syntax, we need it | 
|---|
| 82 | $content = preg_replace('/\{\{tpl:lang\s+(.*?)\}\}/msu','{tpl:lang $1}',$content); | 
|---|
| 83 |  | 
|---|
| 84 | # We remove every {{tpl: | 
|---|
| 85 | $content = preg_replace('/\{\{tpl:.*?\}\}/msu','',$content); | 
|---|
| 86 |  | 
|---|
| 87 | return | 
|---|
| 88 | "<?php publicWidgets::widgetHandler('".addslashes($attr['id'])."','".str_replace("'","\\'",$content)."'); ?>"; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | public static function widgetHandler($id,$xml) | 
|---|
| 92 | { | 
|---|
| 93 | $widgets =& $GLOBALS['__widgets']; | 
|---|
| 94 |  | 
|---|
| 95 | if (!($widgets->{$id} instanceof dcWidget)) { | 
|---|
| 96 | return; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | $xml = '<?xml version="1.0" encoding="utf-8" ?><widget>'.$xml.'</widget>'; | 
|---|
| 100 | $xml = @simplexml_load_string($xml); | 
|---|
| 101 | if (!($xml instanceof SimpleXMLElement)) { | 
|---|
| 102 | echo "Invalid widget XML fragment"; | 
|---|
| 103 | return; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | $w = clone $widgets->{$id}; | 
|---|
| 107 |  | 
|---|
| 108 | foreach ($xml->setting as $e) | 
|---|
| 109 | { | 
|---|
| 110 | if (empty($e['name'])) { | 
|---|
| 111 | continue; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | $setting = (string) $e['name']; | 
|---|
| 115 | if (count($e->children())>0) { | 
|---|
| 116 | $text = preg_replace('#^<setting[^>]*>(.*)</setting>$#msu','\1', (string)$e->asXML()); | 
|---|
| 117 | } else { | 
|---|
| 118 | $text=$e; | 
|---|
| 119 | } | 
|---|
| 120 | $w->{$setting} = preg_replace_callback('/\{tpl:lang (.*?)\}/msu',array('self','widgetL10nHandler'),$text); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | echo $w->call(0); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | private static function widgetL10nHandler($m) | 
|---|
| 127 | { | 
|---|
| 128 | return __($m[1]); | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 | ?> | 
|---|