1<?php 2/** 3 * DokuWiki Plugin letsencrypt (Admin Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9// must be run within Dokuwiki 10use dokuwiki\Form\Form; 11 12if(!defined('DOKU_INC')) die(); 13 14class admin_plugin_letsencrypt extends DokuWiki_Admin_Plugin { 15 16 /** @var helper_plugin_letsencrypt $helper */ 17 protected $helper; 18 19 public function __construct() { 20 $this->helper = plugin_load('helper', 'letsencrypt'); 21 } 22 23 /** 24 * @return int sort number in admin menu 25 */ 26 public function getMenuSort() { 27 return 555; 28 } 29 30 /** 31 * @return bool true if only access for superuser, false is for superusers and moderators 32 */ 33 public function forAdminOnly() { 34 return false; 35 } 36 37 /** 38 * we're doing nothing here 39 * 40 * @inheritDoc 41 */ 42 public function handle() { 43 } 44 45 /** 46 * We're executing everything during rendering, to have a live log 47 */ 48 public function execute() { 49 global $INPUT; 50 51 try { 52 echo '<div class="log">'; 53 $this->helper->setHTMLLogger(); 54 55 if($INPUT->bool('init')) { 56 $countries = $this->getCountries(); 57 $code = $INPUT->str('country'); 58 $country = $countries[$code]; 59 $this->helper->register($code, $country, $INPUT->str('email')); 60 } 61 if($INPUT->bool('sign')) { 62 $this->helper->updateCerts(); 63 } 64 65 echo '</div>'; 66 } catch(\Exception $e) { 67 echo '</div>'; 68 msg($e->getMessage(), -1, $e->getLine(), $e->getFile()); 69 } 70 } 71 72 /** 73 * Render HTML output, e.g. helpful text and a form 74 */ 75 public function html() { 76 echo '<div id="plugin__letsencrypt">'; 77 ptln('<h1>' . $this->getLang('menu') . '</h1>'); 78 79 echo '<div class="log_area">'; 80 $this->execute(); 81 echo '</div>'; 82 83 echo '<dl class="info_area">'; 84 $dirs = $this->html_directories(); 85 $acct = $this->html_account(); 86 $doms = $this->html_domains(); 87 echo '</dl>'; 88 89 echo '<div class="action_area">'; 90 if($dirs) { 91 if($acct) { 92 if($doms) { 93 $this->form_domains(); 94 } 95 } else { 96 $this->form_account(); 97 } 98 } 99 echo '</div>'; 100 101 echo '<div class="doc_area">'; 102 echo $this->locale_xhtml('info'); 103 echo '</div>'; 104 105 echo '</div>'; 106 } 107 108 /** 109 * Output info on the directories 110 * 111 * @return bool directories set up? 112 */ 113 protected function html_directories() { 114 $ok = true; 115 $certdir = $this->helper->getCertDir(); 116 $rootdir = $this->helper->getRoot(); 117 118 echo '<dt>' . $this->getLang('certdir') . '</dt>'; 119 if($certdir) { 120 echo '<dd><code>' . $certdir . '</code></dd>'; 121 } else { 122 echo '<dd class="error">' . $this->getLang('not setup') . '</dd>'; 123 $ok = false; 124 } 125 echo '<dt>' . $this->getLang('rootdir') . '</dt>'; 126 if($rootdir) { 127 echo '<dd><code>' . $rootdir . '</code></dd>'; 128 } else { 129 echo '<dd class="error">' . $this->getLang('not setup') . '</dd>'; 130 $ok = false; 131 } 132 133 return $ok; 134 } 135 136 /** 137 * Output info about the account 138 * 139 * @return bool account available? 140 */ 141 protected function html_account() { 142 echo '<dt>' . $this->getLang('account') . '</dt>'; 143 if($this->helper->hasAccount()) { 144 echo '<dd>' . $this->getLang('set up') . '</dd>'; 145 return true; 146 } else { 147 echo '<dd class="error">' . $this->getLang('not setup') . '</dd>'; 148 return false; 149 } 150 } 151 152 /** 153 * List the detected domains 154 * 155 * @return bool found any domains? 156 */ 157 protected function html_domains() { 158 $domains = $this->helper->getAllDomains(); 159 echo '<dt>' . $this->getLang('domains') . '</dt>'; 160 161 if(!$domains) { 162 echo '<dd class="error">' . $this->getLang('none') . '</dd>'; 163 return false; 164 } 165 166 foreach($domains as $domain => $expire) { 167 echo '<dd>'; 168 echo hsc($domain); 169 170 if($expire > 30) { 171 echo sprintf(' <span class="valid">' . $this->getLang('valid') . '</span>', $expire); 172 } elseif($expire == 0) { 173 echo ' <span class="invalid">' . $this->getLang('invalid') . '</span>'; 174 } else { 175 echo sprintf(' <span class="renew">' . $this->getLang('valid') . '</span>', $expire); 176 } 177 echo '</dd>'; 178 } 179 return true; 180 } 181 182 /** 183 * Form to create new LE account 184 */ 185 protected function form_account() { 186 $license = sprintf($this->getLang('license'), 'https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf'); 187 188 $form = new Form(); 189 $form->addFieldsetOpen($this->getLang('create account')); 190 $form->addTextInput('email', $this->getLang('email'))->addClass('block'); 191 $form->addDropdown('country', $this->getCountries(), $this->getLang('country'))->addClass('block'); 192 $form->addHTML("<p>$license</p>"); 193 $form->addButton('init', $this->getLang('create account'))->attr('type', 'submit')->val(1); 194 echo $form->toHTML(); 195 } 196 197 /** 198 * Form to request Certificates 199 */ 200 protected function form_domains() { 201 $form = new Form(); 202 $form->addFieldsetOpen($this->getLang('get certs')); 203 $form->addButton('sign', $this->getLang('get certs'))->attr('type', 'submit')->val(1); 204 echo $form->toHTML(); 205 } 206 207 /** 208 * @return array 209 */ 210 protected function getCountries() { 211 $out = array(); 212 $raw = file(__DIR__ . '/country-codes.csv'); 213 foreach($raw as $line) { 214 list($country, $code) = explode(',', trim($line)); 215 $out[$code] = $country; 216 } 217 218 return $out; 219 } 220 221} 222 223// vim:ts=4:sw=4:et: 224