printing.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /* Copyright (C) 2013-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014-2015 Frederic France <frederic.france@free.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/printing/admin/printing.php
  20. * \ingroup printing
  21. * \brief Page to setup printing module
  22. */
  23. // Load Dolibarr environment
  24. require '../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/modules/printing/modules_printing.php';
  28. require_once DOL_DOCUMENT_ROOT.'/printing/lib/printing.lib.php';
  29. use OAuth\Common\Storage\DoliStorage;
  30. // Load translation files required by the page
  31. $langs->loadLangs(array('admin', 'printing', 'oauth'));
  32. $action = GETPOST('action', 'aZ09');
  33. $mode = GETPOST('mode', 'alpha');
  34. $value = GETPOST('value', 'alpha', 0, null, null, 1); // The value may be __google__docs so we force disable of replace
  35. $varname = GETPOST('varname', 'alpha');
  36. $driver = GETPOST('driver', 'alpha');
  37. if (!empty($driver)) {
  38. $langs->load($driver);
  39. }
  40. if (!$mode) {
  41. $mode = 'config';
  42. }
  43. $OAUTH_SERVICENAME_GOOGLE = 'Google';
  44. if (!$user->admin) {
  45. accessforbidden();
  46. }
  47. /*
  48. * Action
  49. */
  50. if (($mode == 'test' || $mode == 'setup') && empty($driver)) {
  51. setEventMessages($langs->trans('PleaseSelectaDriverfromList'), null);
  52. header("Location: ".$_SERVER['PHP_SELF'].'?mode=config');
  53. exit;
  54. }
  55. if ($action == 'setconst' && $user->admin) {
  56. $error = 0;
  57. $db->begin();
  58. foreach ($_POST['setupdriver'] as $setupconst) {
  59. //print '<pre>'.print_r($setupconst, true).'</pre>';
  60. $result = dolibarr_set_const($db, $setupconst['varname'], $setupconst['value'], 'chaine', 0, '', $conf->entity);
  61. if (!($result > 0)) {
  62. $error++;
  63. }
  64. }
  65. if (!$error) {
  66. $db->commit();
  67. setEventMessages($langs->trans("SetupSaved"), null);
  68. } else {
  69. $db->rollback();
  70. dol_print_error($db);
  71. }
  72. $action = '';
  73. }
  74. if ($action == 'setvalue' && $user->admin) {
  75. $db->begin();
  76. $result = dolibarr_set_const($db, $varname, $value, 'chaine', 0, '', $conf->entity);
  77. if (!($result > 0)) {
  78. $error++;
  79. }
  80. if (!$error) {
  81. $db->commit();
  82. setEventMessages($langs->trans("SetupSaved"), null);
  83. } else {
  84. $db->rollback();
  85. dol_print_error($db);
  86. }
  87. $action = '';
  88. }
  89. /*
  90. * View
  91. */
  92. $form = new Form($db);
  93. llxHeader('', $langs->trans("PrintingSetup"));
  94. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  95. print load_fiche_titre($langs->trans("PrintingSetup"), $linkback, 'title_setup');
  96. $head = printingAdminPrepareHead($mode);
  97. if ($mode == 'setup' && $user->admin) {
  98. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=setup&amp;driver='.$driver.'" autocomplete="off">';
  99. print '<input type="hidden" name="token" value="'.newToken().'">';
  100. print '<input type="hidden" name="action" value="setconst">';
  101. print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
  102. print $langs->trans("PrintingDriverDesc".$driver)."<br><br>\n";
  103. print '<table class="noborder centpercent">'."\n";
  104. print '<tr class="liste_titre">';
  105. print '<th>'.$langs->trans("Parameters").'</th>';
  106. print '<th>'.$langs->trans("Value").'</th>';
  107. print '<th>&nbsp;</th>';
  108. print "</tr>\n";
  109. $submit_enabled = 0;
  110. if (!empty($driver)) {
  111. if (!empty($conf->modules_parts['printing'])) {
  112. $dirmodels = array_merge(array('/core/modules/printing/'), (array) $conf->modules_parts['printing']);
  113. } else {
  114. $dirmodels = array('/core/modules/printing/');
  115. }
  116. foreach ($dirmodels as $dir) {
  117. if (file_exists(dol_buildpath($dir, 0).$driver.'.modules.php')) {
  118. $classfile = dol_buildpath($dir, 0).$driver.'.modules.php';
  119. break;
  120. }
  121. }
  122. require_once $classfile;
  123. $classname = 'printing_'.$driver;
  124. $printer = new $classname($db);
  125. $langs->load($printer::LANGFILE);
  126. $i = 0;
  127. $submit_enabled = 0;
  128. foreach ($printer->conf as $key) {
  129. switch ($key['type']) {
  130. case "text":
  131. case "password":
  132. print '<tr class="oddeven">';
  133. print '<td'.($key['required'] ? ' class=required' : '').'>'.$langs->trans($key['varname']).'</td>';
  134. print '<td><input size="32" type="'.(empty($key['type']) ? 'text' : $key['type']).'" name="setupdriver['.$i.'][value]" value="'.$conf->global->{$key['varname']}.'"';
  135. print isset($key['moreattributes']) ? ' '.$key['moreattributes'] : '';
  136. print '><input type="hidden" name="setupdriver['.$i.'][varname]" value="'.$key['varname'].'"></td>';
  137. print '<td>&nbsp;'.($key['example'] != '' ? $langs->trans("Example").' : '.$key['example'] : '').'</td>';
  138. print '</tr>'."\n";
  139. break;
  140. case "info": // Google Api setup or Google OAuth Token
  141. print '<tr class="oddeven">';
  142. print '<td'.($key['required'] ? ' class=required' : '').'>';
  143. if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
  144. print $langs->trans("IsTokenGenerated");
  145. } else {
  146. print $langs->trans($key['varname']);
  147. }
  148. print '</td>';
  149. print '<td>'.$langs->trans($key['info']).'</td>';
  150. print '<td>';
  151. //var_dump($key);
  152. if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
  153. // Delete remote tokens
  154. if (!empty($key['delete'])) {
  155. print '<a class="button" href="'.$key['delete'].'">'.$langs->trans('DeleteAccess').'</a><br><br>';
  156. }
  157. // Request remote token
  158. print '<a class="button" href="'.$key['renew'].'">'.$langs->trans('RequestAccess').'</a><br><br>';
  159. // Check remote access
  160. print $langs->trans("ToCheckDeleteTokenOnProvider", $OAUTH_SERVICENAME_GOOGLE).': <a href="https://security.google.com/settings/security/permissions" target="_google">https://security.google.com/settings/security/permissions</a>';
  161. }
  162. print '</td>';
  163. print '</tr>'."\n";
  164. break;
  165. case "submit":
  166. if ($key['enabled']) {
  167. $submit_enabled = 1;
  168. }
  169. break;
  170. }
  171. $i++;
  172. if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
  173. $keyforprovider = ''; // @BUG This must be set
  174. // Token
  175. print '<tr class="oddeven">';
  176. print '<td>'.$langs->trans("Token").'</td>';
  177. print '<td colspan="2">';
  178. $tokenobj = null;
  179. // Dolibarr storage
  180. $storage = new DoliStorage($db, $conf, $keyforprovider);
  181. try {
  182. $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME_GOOGLE);
  183. } catch (Exception $e) {
  184. // Return an error if token not found
  185. }
  186. if (is_object($tokenobj)) {
  187. //var_dump($tokenobj);
  188. print $tokenobj->getAccessToken().'<br>';
  189. //print 'Refresh: '.$tokenobj->getRefreshToken().'<br>';
  190. //print 'EndOfLife: '.$tokenobj->getEndOfLife().'<br>';
  191. //var_dump($tokenobj->getExtraParams());
  192. /*print '<br>Extra: <br><textarea class="quatrevingtpercent">';
  193. print ''.join(',',$tokenobj->getExtraParams());
  194. print '</textarea>';*/
  195. }
  196. print '</td>';
  197. print '</tr>'."\n";
  198. }
  199. }
  200. } else {
  201. print $langs->trans('PleaseSelectaDriverfromList');
  202. }
  203. print '</table>';
  204. print dol_get_fiche_end();
  205. if (!empty($driver)) {
  206. if ($submit_enabled) {
  207. print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Modify")).'"></div>';
  208. }
  209. }
  210. print '</form>';
  211. }
  212. if ($mode == 'config' && $user->admin) {
  213. print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
  214. print $langs->trans("PrintingDesc")."<br><br>\n";
  215. print '<table class="noborder centpercent">'."\n";
  216. print '<tr class="liste_titre">';
  217. print '<th>'.$langs->trans("Description").'</th>';
  218. print '<th class="center">'.$langs->trans("Active").'</th>';
  219. print '<th class="center">'.$langs->trans("Setup").'</th>';
  220. print '<th class="center">'.$langs->trans("TargetedPrinter").'</th>';
  221. print "</tr>\n";
  222. $object = new PrintingDriver($db);
  223. $result = $object->listDrivers($db, 10);
  224. if (!empty($conf->modules_parts['printing'])) {
  225. $dirmodels = array_merge(array('/core/modules/printing/'), (array) $conf->modules_parts['printing']);
  226. } else {
  227. $dirmodels = array('/core/modules/printing/');
  228. }
  229. foreach ($result as $driver) {
  230. foreach ($dirmodels as $dir) {
  231. if (file_exists(dol_buildpath($dir, 0).$driver.'.modules.php')) {
  232. $classfile = dol_buildpath($dir, 0).$driver.'.modules.php';
  233. break;
  234. }
  235. }
  236. require_once $classfile;
  237. $classname = 'printing_'.$driver;
  238. $printer = new $classname($db);
  239. $langs->load($printer::LANGFILE);
  240. //print '<pre>'.print_r($printer, true).'</pre>';
  241. print '<tr class="oddeven">';
  242. print '<td>'.img_picto('', $printer->picto).' '.$langs->trans($printer->desc).'</td>';
  243. print '<td class="center">';
  244. if (!empty($conf->use_javascript_ajax)) {
  245. print ajax_constantonoff($printer->active);
  246. } else {
  247. if (empty($conf->global->{$printer->conf})) {
  248. print '<a href="'.$_SERVER['PHP_SELF'].'?action=setvalue&token='.newToken().'&varname='.urlencode($printer->active).'&value=1">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
  249. } else {
  250. print '<a href="'.$_SERVER['PHP_SELF'].'?action=setvalue&token='.newToken().'&varname='.urlencode($printer->active).'&value=0">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
  251. }
  252. }
  253. print '<td class="center"><a href="'.$_SERVER['PHP_SELF'].'?mode=setup&token='.newToken().'&driver='.urlencode($printer->name).'">'.img_picto('', 'setup').'</a></td>';
  254. print '<td class="center"><a href="'.$_SERVER['PHP_SELF'].'?mode=test&token='.newToken().'&driver='.urlencode($printer->name).'">'.img_picto('', 'setup').'</a></td>';
  255. print '</tr>'."\n";
  256. }
  257. print '</table>';
  258. print dol_get_fiche_end();
  259. }
  260. if ($mode == 'test' && $user->admin) {
  261. print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
  262. print $langs->trans('PrintTestDesc'.$driver)."<br><br>\n";
  263. print '<table class="noborder centpercent">';
  264. if (!empty($driver)) {
  265. if (!empty($conf->modules_parts['printing'])) {
  266. $dirmodels = array_merge(array('/core/modules/printing/'), (array) $conf->modules_parts['printing']);
  267. } else {
  268. $dirmodels = array('/core/modules/printing/');
  269. }
  270. foreach ($dirmodels as $dir) {
  271. if (file_exists(dol_buildpath($dir, 0).$driver.'.modules.php')) {
  272. $classfile = dol_buildpath($dir, 0).$driver.'.modules.php';
  273. break;
  274. }
  275. }
  276. require_once $classfile;
  277. $classname = 'printing_'.$driver;
  278. $langs->load($driver);
  279. $printer = new $classname($db);
  280. $langs->load($printer::LANGFILE);
  281. //print '<pre>'.print_r($printer, true).'</pre>';
  282. if (count($printer->getlistAvailablePrinters())) {
  283. if ($printer->listAvailablePrinters() == 0) {
  284. print $printer->resprint;
  285. } else {
  286. setEventMessages($printer->error, $printer->errors, 'errors');
  287. }
  288. } else {
  289. print $langs->trans('PleaseConfigureDriverfromList');
  290. }
  291. } else {
  292. print $langs->trans('PleaseSelectaDriverfromList');
  293. }
  294. print '</table>';
  295. print dol_get_fiche_end();
  296. }
  297. if ($mode == 'userconf' && $user->admin) {
  298. print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
  299. print $langs->trans('PrintUserConfDesc'.$driver)."<br><br>\n";
  300. print '<table class="noborder centpercent">';
  301. print '<tr class="liste_titre">';
  302. print '<th>'.$langs->trans("User").'</th>';
  303. print '<th>'.$langs->trans("PrintModule").'</th>';
  304. print '<th>'.$langs->trans("PrintDriver").'</th>';
  305. print '<th>'.$langs->trans("Printer").'</th>';
  306. print '<th>'.$langs->trans("PrinterLocation").'</th>';
  307. print '<th>'.$langs->trans("PrinterId").'</th>';
  308. print '<th>'.$langs->trans("NumberOfCopy").'</th>';
  309. print '<th class="center">'.$langs->trans("Delete").'</th>';
  310. print "</tr>\n";
  311. $sql = 'SELECT p.rowid, p.printer_name, p.printer_location, p.printer_id, p.copy, p.module, p.driver, p.userid, u.login FROM '.MAIN_DB_PREFIX.'printing as p, '.MAIN_DB_PREFIX.'user as u WHERE p.userid=u.rowid';
  312. $resql = $db->query($sql);
  313. while ($row = $db->fetch_array($resql)) {
  314. print '<tr class="oddeven">';
  315. print '<td>'.$row['login'].'</td>';
  316. print '<td>'.$row['module'].'</td>';
  317. print '<td>'.$row['driver'].'</td>';
  318. print '<td>'.$row['printer_name'].'</td>';
  319. print '<td>'.$row['printer_location'].'</td>';
  320. print '<td>'.$row['printer_id'].'</td>';
  321. print '<td>'.$row['copy'].'</td>';
  322. print '<td class="center">'.img_picto($langs->trans("Delete"), 'delete').'</td>';
  323. print "</tr>\n";
  324. }
  325. print '</table>';
  326. print dol_get_fiche_end();
  327. }
  328. // End of page
  329. llxFooter();
  330. $db->close();