OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / templates / _admin3 / functions.php
1 <?php
2
3 defined('_JEXEC') or die;
4
5 if (!defined('_ARTX_FUNCTIONS')) {
6
7     define('_ARTX_FUNCTIONS', 1);
8         define('_M3_BUTTON_CLASS', 'btn');              // Magic3用のボタンクラス
9
10     $GLOBALS['artx_settings'] = array(
11         'block' => array('has_header' => true),
12         'menu' => array('show_submenus' => true),
13         'vmenu' => array('show_submenus' => false, 'simple' => false)
14     );
15
16     require_once dirname(__FILE__) . '/library/Artx.php';
17
18     /**
19      * Decorates an article or block with the art-post style.
20      *
21      * Elements of the $data array:
22      *  'classes'
23      *  'header-text'
24      *  'header-icon'
25      *  'header-link'
26      *  'metadata-header-icons'
27      *  'metadata-footer-icons'
28      *  'content'
29      */
30     function artxPost($data)
31     {
32         if (is_string($data))
33             $data = array('content' => $data);
34         $classes = isset($data['classes']) && strlen($data['classes']) ? $data['classes'] : '';
35                     artxFragmentBegin("<article class=\"art-post" . $classes . "\">");
36             artxFragmentBegin("<h2 class=\"art-postheader\">");
37             if (isset($data['header-text']) && strlen($data['header-text'])) {
38                 if (isset($data['header-link']) && strlen($data['header-link']))
39                     artxFragmentContent('<a href="' . $data['header-link'] . '">' . $data['header-text'] . '</a>');
40                 else
41                     artxFragmentContent($data['header-text']);
42             }
43             artxFragmentEnd("</h2>");
44             artxFragmentBegin("<div class=\"art-postheadericons art-metadata-icons\">");
45             if (isset($data['metadata-header-icons']) && count($data['metadata-header-icons']))
46                 foreach ($data['metadata-header-icons'] as $icon)
47                     artxFragment('', $icon, '', ' | ');
48             artxFragmentEnd("</div>");
49             artxFragmentBegin("<div class=\"art-postcontent clearfix\">");
50             if (isset($data['content']) && strlen($data['content']))
51                 artxFragmentContent(artxPostprocessPostContent($data['content']));
52             artxFragmentEnd("</div>");
53             artxFragmentBegin("<div class=\"art-postfootericons art-metadata-icons\">");
54             if (isset($data['metadata-footer-icons']) && count($data['metadata-footer-icons']))
55                 foreach ($data['metadata-footer-icons'] as $icon)
56                     artxFragment('', $icon, '', ' | ');
57             artxFragmentEnd("</div>");
58
59             return artxFragmentEnd("</article>", '', true);
60
61     }
62
63     function artxBlock($caption, $content, $classes = '')
64     {
65         $hasCaption = ($GLOBALS['artx_settings']['block']['has_header']
66             && null !== $caption && strlen(trim($caption)) > 0);
67         $hasContent = (null !== $content && strlen(trim($content)) > 0);
68         if (!$hasCaption && !$hasContent) return '';
69                 
70                 /*
71                 artxFragmentBegin("<div class=\"art-block clearfix" . $classes . "\">");
72                 artxFragmentBegin("<div class=\"art-blockheader\"><h3 class=\"t\">");
73                 artxFragmentContent($caption);
74                 artxFragmentEnd("</h3></div>");
75                 artxFragmentBegin("<div class=\"art-blockcontent\">");
76                 artxFragmentContent(artxPostprocessBlockContent($content));
77                 artxFragmentEnd("</div>");*/
78                 
79                 artxFragmentBegin('<div class="m3widget_box ui-widget">');
80                 artxFragmentBegin('<div class="m3widget_box_head ui-state-default ui-priority-primary ui-corner-tl ui-corner-tr">');
81                 artxFragmentContent($caption);
82                 artxFragmentEnd('</div>');
83                 artxFragmentBegin('<div class="m3widget_box_content ui-widget-content ui-corner-bl ui-corner-br">');
84                 artxFragmentContent(artxPostprocessBlockContent($content));
85                 artxFragmentEnd('</div>');
86                 return artxFragmentEnd("</div>", '', true);
87     }
88
89     
90
91     function artxUrlToHref($url)
92     {
93         $result = '';
94         $p = parse_url($url);
95         if (isset($p['scheme']) && isset($p['host'])) {
96             $result = $p['scheme'] . '://';
97             if (isset($p['user'])) {
98                 $result .= $p['user'];
99                 if (isset($p['pass']))
100                     $result .= ':' . $p['pass'];
101                 $result .= '@';
102             }
103             $result .= $p['host'];
104             if (isset($p['port']))
105                 $result .= ':' . $p['port'];
106             if (!isset($p['path']))
107                 $result .= '/';
108         }
109         if (isset($p['path']))
110             $result .= $p['path'];
111         if (isset($p['query'])) {
112             $result .= '?' . str_replace('&', '&amp;', $p['query']);
113         }
114         if (isset($p['fragment']))
115             $result .= '#' . $p['fragment'];
116         return $result;
117     }
118
119     /**
120      * Searches $content for tags and returns information about each found tag.
121      *
122      * Created to support button replacing process, e.g. wrapping submit/reset
123      * inputs and buttons with artisteer style.
124      *
125      * When all the $name tags are found, they are processed by the $filter specified.
126      * Filter is applied to the attributes. When an attribute contains several values
127      * (e.g. class attribute), only tags that contain all the values from filter
128      * will be selected. E.g. filtering by the button class will select elements
129      * with class="button" and class="button validate".
130      *
131      * Parsing does not support nested tags. Looking for span tags in
132      * <span>1<span>2</span>3</span> will return <span>1<span>2</span> and
133      * <span>2</span>.
134      *
135      * Examples:
136      *  Select all tags with class='readon':
137      *   artxFindTags($html, array('*' => array('class' => 'readon')))
138      *  Select inputs with type='submit' and class='button':
139      *   artxFindTags($html, array('input' => array('type' => 'submit', 'class' => 'button')))
140      *  Select inputs with type='submit' and class='button validate':
141      *   artxFindTags($html, array('input' => array('type' => 'submit', 'class' => array('button', 'validate'))))
142      *  Select inputs with class != 'art-button'
143      *   artxFindTags($html, array('input' => array('class' => '!art-button')))
144      *  Select inputs with non-empty class
145      *   artxFindTags($html, array('input' => array('class' => '!')))
146      *  Select inputs with class != 'art-button' and non-empty class:
147      *   artxFindTags($html, array('input' => array('class' => array('!art-button', '!'))))
148      *  Select inputs with class = button but != 'art-button'
149      *   artxFindTags($html, array('input' => array('class' => array('button', '!art-button'))))
150      *
151      * @return array of text fragments and tag information: position, length,
152      *         name, attributes, raw_open_tag, body.
153      */
154     function artxFindTags($content, $filters)
155     {
156         $result = array('');
157         $index = 0;
158         $position = 0;
159         $name = implode('|', array_keys($filters));
160         $name = str_replace('*', '\w+', $name);
161         // search for open tag
162         if (preg_match_all(
163             '~<(' . $name . ')\b(?:\s+[^\s]+\s*=\s*(?:"[^"]+"|\'[^\']+\'|[^\s>]+))*\s*/?' . '>~i', $content,
164             $tagMatches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER))
165         {
166             foreach ($tagMatches as $tagMatch) {
167                 $rawMatch = $tagMatch[0][0];
168                 $name = $tagMatch[1][0];
169                 $normalName = strtolower($name);
170                 $tag = array('name' => $name, 'position' => $tagMatch[0][1]);
171                 $openTagTail = (strlen($rawMatch) > 1 && '/' == $rawMatch[strlen($rawMatch) - 2])
172                     ? '/>' : '>';
173                 // different instructions for paired and unpaired tags
174                 switch ($normalName)
175                 {
176                     case 'input':
177                     case 'img':
178                     case 'br':
179                         $tag['paired'] = false;
180                         $tag['length'] = strlen($tagMatch[0][0]);
181                         $tag['body'] = null;
182                         $tag['close'] = 2 == strlen($openTagTail);
183                         break;
184                     default:
185                         $closeTag = '</' . $name . '>';
186                         $closeTagLength = strlen($closeTag);
187                         $tag['paired'] = true;
188                         $end = strpos($content, $closeTag, $tag['position']);
189                         if (false === $end)
190                             continue;
191                         $openTagLength = strlen($tagMatch[0][0]);
192                         $tag['body'] = substr($content, $tag['position'] + $openTagLength,
193                             $end - $openTagLength - $tag['position']);
194                         $tag['length'] = $end + $closeTagLength - $tag['position'];
195                         break;
196                 }
197                 // parse attributes
198                 $rawAttributes = trim(substr($tagMatch[0][0], strlen($name) + 1,
199                     strlen($tagMatch[0][0]) - strlen($name) - 1 - strlen($openTagTail)));
200                 $attributes = array();
201                 if (preg_match_all('~([^=\s]+)\s*=\s*(?:(")([^"]+)"|(\')([^\']+)\'|([^\s]+))~',
202                     $rawAttributes, $attributeMatches, PREG_SET_ORDER))
203                 {
204                     foreach ($attributeMatches as $attrMatch) {
205                         $attrName = $attrMatch[1];
206                         $attrDelimeter = (isset($attrMatch[2]) && '' !== $attrMatch[2])
207                             ? $attrMatch[2]
208                             : ((isset($attrMatch[4]) && '' !== $attrMatch[4])
209                                 ? $attrMatch[4] : '');
210                         $attrValue = (isset($attrMatch[3]) && '' !== $attrMatch[3])
211                             ? $attrMatch[3]
212                             : ((isset($attrMatch[5]) && '' !== $attrMatch[5])
213                                 ? $attrMatch[5] : $attrMatch[6]);
214                         if ('class' == $attrName)
215                             $attrValue = explode(' ', preg_replace('~\s+~', ' ', $attrValue));
216                         $attributes[$attrName] = array('delimeter' => $attrDelimeter,
217                             'value' => $attrValue);
218                     }
219                 }
220                 // apply filter to attributes
221                 $passed = true;
222                 $filter = isset($filters[$normalName])
223                     ? $filters[$normalName]
224                     : (isset($filters['*']) ? $filters['*'] : array());
225                 foreach ($filter as $key => $value) {
226                     $criteria = is_array($value) ? $value : array($value);
227                     for ($c = 0; $c < count($criteria) && $passed; $c++) {
228                         $crit = $criteria[$c];
229                         if ('' == $crit) {
230                             // attribute should be empty
231                             if ('class' == $key) {
232                                 if (isset($attributes[$key]) && count($attributes[$key]['value']) != 0) {
233                                     $passed = false;
234                                     break;
235                                 }
236                             } else {
237                                 if (isset($attributes[$key]) && strlen($attributes[$key]['value']) != 0) {
238                                     $passed = false;
239                                     break;
240                                 }
241                             }
242                         } else if ('!' == $crit) {
243                             // attribute should be not set or empty
244                             if ('class' == $key) {
245                                 if (!isset($attributes[$key]) || count($attributes[$key]['value']) == 0) {
246                                     $passed = false;
247                                     break;
248                                 }
249                             } else {
250                                 if (!isset($attributes[$key]) || strlen($attributes[$key]['value']) == 0) {
251                                     $passed = false;
252                                     break;
253                                 }
254                             }
255                         } else if ('!' == $crit[0]) {
256                             // * attribute should not contain value
257                             // * if attribute is empty, it does not contain value
258                             if ('class' == $key) {
259                                 if (isset($attributes[$key]) && count($attributes[$key]['value']) != 0
260                                     && in_array(substr($crit, 1), $attributes[$key]['value']))
261                                 {
262                                     $passed = false;
263                                     break;
264                                 }
265                             } else {
266                                 if (isset($attributes[$key]) && strlen($attributes[$key]['value']) != 0
267                                     && $crit == $attributes[$key]['value'])
268                                 {
269                                     $passed = false;
270                                     break;
271                                 }
272                             }
273                         } else {
274                             // * attribute should contain value
275                             // * if attribute is empty, it does not contain value
276                             if ('class' == $key) {
277                                 if (!isset($attributes[$key]) || count($attributes[$key]['value']) == 0) {
278                                     $passed = false;
279                                     break;
280                                 }
281                                 if (!in_array($crit, $attributes[$key]['value'])) {
282                                     $passed = false;
283                                     break;
284                                 }
285                             } else {
286                                 if (!isset($attributes[$key]) || strlen($attributes[$key]['value']) == 0) {
287                                     $passed = false;
288                                     break;
289                                 }
290                                 if ($crit != $attributes[$key]['value']) {
291                                     $passed = false;
292                                     break;
293                                 }
294                             }
295                         }
296                     }
297                     if (!$passed)
298                         break;
299                 }
300                 if (!$passed)
301                     continue;
302                 // finalize tag info constrution
303                 $tag['attributes'] = $attributes;
304                 $result[$index] = substr($content, $position, $tag['position'] - $position);
305                 $position = $tag['position'] + $tag['length'];
306                 $index++;
307                 $result[$index] = $tag;
308                 $index++;
309             }
310         }
311         $result[$index] = $position < strlen($content) ? substr($content, $position) : '';
312         return $result;
313     }
314
315     /**
316      * Converts tag info created by artxFindTags back to text tag.
317      *
318      * @return string
319      */
320     function artxTagInfoToString($info)
321     {
322         $result = '<' . $info['name'];
323         if (isset($info['attributes']) && 0 != count($info['attributes'])) {
324             $attributes = '';
325             foreach ($info['attributes'] as $key => $value)
326                 $attributes .= ' ' . $key . '=' . $value['delimeter']
327                     . (is_array($value['value']) ? implode(' ', $value['value']) : $value['value'])
328                     . $value['delimeter'];
329             $result .= $attributes;
330         }
331         if ($info['paired']) {
332             $result .= '>';
333             $result .= $info['body'];
334             $result .= '</' . $info['name'] . '>';
335         } else
336             $result .= ($info['close'] ? ' /' : '') . '>';
337         return $result;
338     }
339
340     /**
341      * Decorates the specified tag with artisteer button style.
342      *
343      * @param string $name tag name that should be decorated
344      * @param array $filter select $name tags with attributes matching $filter
345      * @return $content with replaced $name tags
346      */
347     function artxReplaceButtons($content, $filter = array('input' => array('class' => 'button')))
348     {
349         $result = '';
350         foreach (artxFindTags($content, $filter) as $tag) {
351             if (is_string($tag))
352                 $result .= $tag;
353             else {
354                 //$tag['attributes']['class']['value'][] = 'art-button';                // 追加しない
355                                 $tag['attributes']['class']['value'] = array(_M3_BUTTON_CLASS);         // ボタンクラスを変更
356                 $result .= artxTagInfoToString($tag);
357             }
358         }
359         return $result;
360     }
361
362     function artxLinkButton($data = array())
363     {
364         return '<a class="' . (isset($data['classes']) && isset($data['classes']['a']) ? $data['classes']['a'] . ' ' : '')
365             . 'art-button" href="' . $data['link'] . '">' . $data['content'] . '</a>';
366     }
367
368     function artxHtmlFixFormAction($content)
369     {
370         if (preg_match('~ action="([^"]+)" ~', $content, $matches, PREG_OFFSET_CAPTURE)) {
371             $content = substr($content, 0, $matches[0][1])
372                 . ' action="' . artxUrlToHref($matches[1][0]) . '" '
373                 . substr($content, $matches[0][1] + strlen($matches[0][0]));
374         }
375         return $content;
376     }
377
378     function artxTagBuilder($tag, $attributes = array(), $content = '') {
379         $result = '<' . $tag;
380         foreach ($attributes as $name => $value) {
381             if (is_string($value)) {
382                 if (!empty($value))
383                     $result .= ' ' . $name . '="' . $value . '"';
384             } else if (is_array($value)) {
385                 $values = array_filter($value);
386                 if (count($values))
387                     $result .= ' ' . $name . '="' . implode(' ', $value) . '"';
388             }
389         }
390         $result .= '>' . $content . '</' . $tag . '>';
391         return $result;
392     }
393
394     $artxFragments = array();
395
396     function artxFragmentBegin($head = '')
397     {
398         global $artxFragments;
399         $artxFragments[] = array('head' => $head, 'content' => '', 'tail' => '');
400     }
401
402     function artxFragmentContent($content = '')
403     {
404         global $artxFragments;
405         $artxFragments[count($artxFragments) - 1]['content'] = $content;
406     }
407
408     function artxFragmentEnd($tail = '', $separator = '', $return = false)
409     {
410         global $artxFragments;
411         $fragment = array_pop($artxFragments);
412         $fragment['tail'] = $tail;
413         $content = trim($fragment['content']);
414         if (count($artxFragments) == 0) {
415             if ($return)
416                 return (trim($content) == '') ? '' : ($fragment['head'] . $content . $fragment['tail']);
417             echo (trim($content) == '') ? '' : ($fragment['head'] . $content . $fragment['tail']);
418         } else {
419             $result = (trim($content) == '') ? '' : ($fragment['head'] . $content . $fragment['tail']);
420             $fragment =& $artxFragments[count($artxFragments) - 1];
421             $fragment['content'] .= (trim($fragment['content']) == '' ? '' : $separator) . $result;
422         }
423     }
424
425     function artxFragment($head = '', $content = '', $tail = '', $separator = '', $return = false)
426     {
427         global $artxFragments;
428         if ($head != '' && $content == '' && $tail == '' && $separator == '') {
429             $content = $head;
430             $head = '';
431         } elseif ($head != '' && $content != '' && $tail == '' && $separator == '') {
432             $separator = $content;
433             $content = $head;
434             $head = '';
435         }
436         artxFragmentBegin($head);
437         artxFragmentContent($content);
438         artxFragmentEnd($tail, $separator, $return);
439     }
440
441     function artxPostprocessBlockContent($content)
442     {
443         return artxPostprocessContent($content);
444     }
445
446     function artxPostprocessPostContent($content)
447     {
448         return artxPostprocessContent($content);
449     }
450
451     function artxPostprocessContent($content)
452     {
453 //        $config = JFactory::getConfig();
454 //        $sef = method_exists($config, 'getValue') ? $config->getValue('config.sef') : $sef = $config->get('config.sef');
455 //        if ($sef) $content = str_replace('url(\'images/', 'url(\'' . JURI::base(true) . '/images/', $content);
456 //                      $content = artxReplaceButtons($content, array('input' => array('class' => array('button', '!art-button')),
457 //                      'button' => array('class' => array('button', '!art-button'))));
458         return $content;
459     }
460
461     function artxBalanceTags($text) {
462        
463         $singleTags = array('area', 'base', 'basefont', 'br', 'col', 'command', 'embed', 'frame', 'hr', 'img', 'input', 'isindex', 'link', 'meta', 'param', 'source');
464         $nestedTags = array('blockquote', 'div', 'object', 'q', 'span');
465         
466         $stack = array();
467         $size = 0;
468         $queue = '';
469         $output = '';
470
471         while (preg_match("/<(\/?[\w:]*)\s*([^>]*)>/", $text, $match)) {
472             $output .= $queue;
473
474             $i = strpos($text, $match[0]);
475             $l = strlen($match[0]);
476
477             $queue = '';
478                         
479             if (isset($match[1][0]) && '/' == $match[1][0]) {
480                 // processing of the end tag
481                 $tag = strtolower(substr($match[1],1));
482
483                 if($size <= 0) {
484                     $tag = '';
485                 } else if ($stack[$size - 1] == $tag) {
486                     $tag = '</' . $tag . '>';
487                     array_pop($stack);
488                     $size--;
489                 } else {
490                     for ($j = $size-1; $j >= 0; $j--) {
491                         if ($stack[$j] == $tag) {
492                             for ($k = $size-1; $k >= $j; $k--) {
493                                 $queue .= '</' . array_pop($stack) . '>';
494                                 $size--;
495                             }
496                             break;
497                         }
498                     }
499                     $tag = '';
500                 }
501             } else { 
502                 // processing of the begin tag
503                 $tag = strtolower($match[1]);
504
505                 if (substr($match[2], -1) == '/') {
506                     if (!in_array($tag, $singleTags))
507                         $match[2] = trim(substr($match[2], 0, -1)) . "></$tag";
508                 } elseif (in_array($tag, $singleTags)) {
509                     $match[2] .= '/';
510                 } else {
511                     if ($size > 0 && !in_array($tag, $nestedTags) && $stack[$size - 1] == $tag) {
512                         $queue = '</' . array_pop($stack) . '>';
513                         $size--;
514                     }
515                     $size = array_push($stack, $tag);
516                 }
517
518                 // attributes
519                 $attributes = $match[2];
520                 if(!empty($attributes) && $attributes[0] != '>')
521                     $attributes = ' ' . $attributes;
522
523                 $tag = '<' . $tag . $attributes . '>';
524                 
525                 if (!empty($queue)) {
526                     $queue .= $tag;
527                     $tag = '';
528                 }
529             }
530             $output .= substr($text, 0, $i) . $tag;
531             $text = substr($text, $i + $l);
532         }
533
534         $output .= ($queue . $text);
535
536         while($t = array_pop($stack))
537             $output .= '</' . $t . '>';
538
539         return $output;
540     }
541 }