1<?php
2/**
3 * Federated Login for DokuWiki - provider class
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @link       http://www.dokuwiki.org/plugin:fedauth
7 * @author     Aoi Karasu <aoikarasu@gmail.com>
8 */
9
10if(!defined('FA_PROVIDER_INC')) define('FA_PROVIDER_INC', __FILE__);
11
12/**
13 * The provider class stores all the configuration information related
14 * to single authorization provider except its display size and order.
15 *
16 * The general purpose of this class is to make the provider data read-only
17 * for all extenral access and provide the serialization usable with
18 * the configuration file.
19 *
20 * @author Aoi Karasu <aoikarasu@gmail.com>
21 */
22class fa_provider {
23
24    var $id = '';
25    var $data = array();
26
27    public static function create($id, $data) {
28        $instance = new self();
29        $instance->loadProvider($id, $data);
30        return $instance;
31    }
32
33    public static function createCustom($id, $name, $url, $large, $small) {
34        $data = array(
35            'name' => $name,
36            'url' => $url,
37            'img_large' => $large,
38            'img_small' => $small,
39            'disabled' => 0,
40            'custom' => 1
41            );
42
43        return self::create($id, $data);
44    }
45
46    protected function loadProvider($id, $data) {
47        $this->id = $id;
48        $this->data = $data;
49        $this->data['use_uname'] = strstr($data['url'], '{username}') ? 1 : 0;
50    }
51
52    public function enable() {
53        $this->data['disabled'] = 0;
54    }
55
56    public function disable() {
57        $this->data['disabled'] = 1;
58    }
59
60    /**
61     * Toggles enabled state of the provider.
62     *
63     * @param mixed $onoff true - enable, false - disable, null - automatic change
64     * @return bool true, if the state has been changed
65     */
66    public function toggle($onoff=null) {
67        if (is_null($onoff)) {
68            $this->data['disabled'] = $this->isEnabled() ? 1 : 0;
69            return true;
70        }
71        $old = $this->data['disabled'];
72        $this->data['disabled'] = $onoff ? 0 : 1;
73        return ($old != $this->data['disabled']);
74    }
75
76    public function isEnabled() {
77        return $this->data['disabled'] != 1;
78    }
79
80    public function isRemovable() {
81        return $this->data['custom'] != 0;
82    }
83
84    public function getId() {
85        return $this->id;
86    }
87
88    /**
89     * Builds XHTML for selected image.
90     */
91    public function getImageXHTML($size=PROV_SMALL, $class='floatimg') {
92        // select image source and size
93        if ($size != PROV_SMALL) {
94            $params = array('w' => 80, 'h' => 40);
95            $src  = $this->data['img_large'];
96        } else {
97            $params = array('w' => 16, 'h' => 16);
98            $src  = $this->data['img_small'];
99        }
100        // prepare image URL
101        if (substr($src, 0, 1) === '@') {
102            $src = str_replace('@DEF@', getBaseURL() .'lib/plugins/fedauth/images/', $src); // local image
103        } else {
104            // media link
105            $mfn = mediaFN(':'.$src);
106            if (@file_exists($mfn)) {
107                $link = ml(':'.$src, $params);
108                return $link;
109            }
110            // media not found, use _noimage
111            $src = getBaseURL() .'lib/plugins/fedauth/images/' . ($size != PROV_SMALL ? 'large/' : '')  . '_noimage.png';
112        }
113        return '<img src="'.$src.'" class="'.$class.'" width="'.$params['w'].'" height="'.$params['h'].'" title="'.$this->getName().'" />';
114    }
115
116    public function getName() {
117        return $this->data['name'];
118    }
119
120    /**
121     * Returns the authorization service URL.
122     */
123    public function getURL() {
124        return $this->data['url'];
125    }
126
127    /**
128     * Returns the authorization provider data serialized using PHP syntax.
129     * This is an utility method for saving the configuration to file.
130     *
131     * @param string $varname name of the variable used for the serialization
132     * @return string serialized provider data
133     */
134    public function getSerialized($varname='fa_providers') {
135        $str = '';
136        foreach ($this->data as $key => $val) {
137            if ($key == 'use_uname') continue;
138            $str .= sprintf("\$%s['%s']['%s'] = %s;\n", $varname, $this->id,
139                $key, is_numeric($val) ? $val : "'" . $val . "'");
140        }
141        return $str;
142    }
143
144    public function hasUsername() {
145        return $this->data['use_uname'] != 0;
146    }
147
148    /**
149     * Sets some details, if this is a built-in provider loaded from default config.
150     *
151     * Note: Instead of hardlinks a @DEF@ string is included for dynamic generation
152     *       of the image URL. This is enables support for multi-domain wikis and
153     *       preserves image links in case wiki root was moved manually.
154     *
155     * @param string $imgpath local path to built-in provider images
156     */
157    public function setupDetails($imgpath) {
158        if ($this->data['custom']) return;
159        $large = 'large/' . $this->id . '.png';
160        $small = $this->id . '.png';
161        $this->data['img_large'] = '@DEF@' . (file_exists($imgpath . $large) ? $large  : 'large/_noimage.png');
162        $this->data['img_small'] = '@DEF@' . (file_exists($imgpath . $small) ? $small  : '_noimage.png');
163    }
164
165} /* fa_provider */
166
167/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
168