1<?php 2/** 3 * Federated Login for DokuWiki - common utility functions 4 * 5 * Enables your DokuWiki to provide users with 6 * Hybrid OAuth + OpenId federated login. 7 * 8 * @copyright 2012 Aoi Karasu 9 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 10 * @link http://www.dokuwiki.org/plugin:fedauth 11 * @author Aoi Karasu <aoikarasu@gmail.com> 12 */ 13 14if (!defined('FEDAUTH_PLUGIN')) die(); 15 16/** 17 * Implodes an array with the key and value pair giving 18 * a glue, a separator between pairs and flattens the nested 19 * array using square braces, eg. topkey[nested1][nested2]. 20 * 21 * Params b1 and b2 are key wprappers for recursive array scan. 22 * Do not use them, unless you want to wrap top level keys. 23 * 24 * @param array $array the array to implode 25 * @param string $glue glue between key and value 26 * @param string $separator separator between pairs 27 * @return string the imploded array 28 * 29 * @author Aoi Karasu <aoikarasu@gmail.com> 30 * @author Original by lightningspirit <lightningspirit@gmail.com> 31 */ 32function array_implode($array, $glue='=', $separator='&', $b1='',$b2='') { 33 if (!is_array($array)) return $array; 34 $string = array(); 35 foreach ($array as $key => $val) { 36 if (is_array($val)) { 37 $val = array_implode($val, $glue, $separator.$b1.$key.$b2, '[', ']'); 38 $string[] = $b1.$key.$b2.$val; 39 } else 40 $string[] = $b1.$key.$b2.$glue.$val; 41 } 42 return implode($separator, $string); 43} 44 45/** 46 * Renders XHTML script block with contents loaded from a plugin-local 47 * JavaScript file. The file location is supposed to be 'js/$name.js'. 48 * All comments (including the docblock) are removed by default. 49 * 50 * @param string $name the name of the JavaScript file without path nor extension 51 * @return string XHTML scrpit block or empty string, if file not found 52 */ 53function plugin_script_block($name, $nocomments=true) { 54 $jsfile = FEDAUTH_PLUGIN . 'js/' . $name . '.js'; 55 if (!file_exists($jsfile)) return ''; 56 57 $text = @file_get_contents($jsfile); 58 // remove all comments from the file 59 if ($nocomments) { 60 $text = preg_replace('!/\*.*?\*/!s', '', $text); 61 $text = preg_replace('/\n\s*\n/', "\n", $text); 62 } 63 $out = '<script type="text/javascript" charset="utf-8"><!--//--><![CDATA[//><!--' . "\n" 64 . $text . "\n" . '//--><!]]></script>' . "\n"; 65 return $out; 66} 67 68/** 69 * Loads a handler class. 70 * 71 * @param object $plugin owner of the handler instance 72 * @param string $cmd command to handle 73 * @param string $type scope suffix beeing a part of class' filename 74 * @param string $provid (optional) an authorization provider id 75 */ 76function &load_handler_class($plugin, $cmd, $type, $provid='', $base='base') { 77 $class = "fa_" . $cmd; 78 79 // don't want require_once end up dying, load base class instead 80 $hfile = FEDAUTH_PLUGIN . "classes/$type/$class.$type.class.php"; 81 if (file_exists($hfile)) { 82 require_once($hfile); 83 } 84 if (!class_exists($class)) { 85 $class = "fa_" . $base; 86 if (!class_exists($class)) { 87 throw new RuntimeException("Base class $class must be included before load_handler_class() is called."); 88 } 89 } 90 91 return new $class($plugin, $cmd, $provid); 92} 93 94function request_url() { 95 $s = empty($_SERVER['HTTPS']) ? '' : ($_SERVER['HTTPS'] == 'on') ? 's' : ''; 96 $protocol = substr(strtolower($_SERVER['SERVER_PROTOCOL']), 0, strpos(strtolower($_SERVER['SERVER_PROTOCOL']), '/')) . $s; 97 $defp = empty($s) ? '80' : '443'; 98 $port = ($_SERVER['SERVER_PORT'] == $defp) ? '' : (':'.$_SERVER['SERVER_PORT']); 99 return $protocol . '://' . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI']; 100} 101 102/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 103