emailcollector_card.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. <?php
  2. /* Copyright (C) 2018 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2022 Charlene Benke <charlene@patas-monkey.com>
  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/admin/emailcollector_card.php
  20. * \ingroup emailcollector
  21. * \brief Page to create/edit/view emailcollector
  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/lib/agenda.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/events.class.php';
  28. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  29. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  30. include_once DOL_DOCUMENT_ROOT.'/emailcollector/class/emailcollector.class.php';
  31. include_once DOL_DOCUMENT_ROOT.'/emailcollector/class/emailcollectorfilter.class.php';
  32. include_once DOL_DOCUMENT_ROOT.'/emailcollector/class/emailcollectoraction.class.php';
  33. include_once DOL_DOCUMENT_ROOT.'/emailcollector/lib/emailcollector.lib.php';
  34. use Webklex\PHPIMAP\ClientManager;
  35. use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
  36. use Webklex\PHPIMAP\Exceptions\InvalidWhereQueryCriteriaException;
  37. use OAuth\Common\Storage\DoliStorage;
  38. use OAuth\Common\Consumer\Credentials;
  39. if (!$user->admin) {
  40. accessforbidden();
  41. }
  42. if (!isModEnabled('emailcollector')) {
  43. accessforbidden();
  44. }
  45. // Load traductions files required by page
  46. $langs->loadLangs(array("admin", "mails", "other"));
  47. // Get parameters
  48. $id = GETPOST('id', 'int');
  49. $ref = GETPOST('ref', 'alpha');
  50. $action = GETPOST('action', 'aZ09');
  51. $confirm = GETPOST('confirm', 'alpha');
  52. $cancel = GETPOST('cancel', 'aZ09');
  53. $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'emailcollectorcard'; // To manage different context of search
  54. $backtopage = GETPOST('backtopage', 'alpha');
  55. $operationid = GETPOST('operationid', 'int');
  56. // Initialize technical objects
  57. $object = new EmailCollector($db);
  58. $extrafields = new ExtraFields($db);
  59. $diroutputmassaction = $conf->emailcollector->dir_output.'/temp/massgeneration/'.$user->id;
  60. $hookmanager->initHooks(array('emailcollectorcard')); // Note that conf->hooks_modules contains array
  61. // Fetch optionals attributes and labels
  62. $extrafields->fetch_name_optionals_label($object->table_element);
  63. $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
  64. // Initialize array of search criterias
  65. $search_all = GETPOST("search_all", 'alpha');
  66. $search = array();
  67. foreach ($object->fields as $key => $val) {
  68. if (GETPOST('search_'.$key, 'alpha')) {
  69. $search[$key] = GETPOST('search_'.$key, 'alpha');
  70. }
  71. }
  72. if (GETPOST('saveoperation2')) {
  73. $action = 'updateoperation';
  74. }
  75. if (empty($action) && empty($id) && empty($ref)) {
  76. $action = 'view';
  77. }
  78. // Load object
  79. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
  80. // Security check - Protection if external user
  81. //if ($user->socid > 0) accessforbidden();
  82. //if ($user->socid > 0) $socid = $user->socid;
  83. //$isdraft = (($object->statut == MyObject::STATUS_DRAFT) ? 1 : 0);
  84. //$result = restrictedArea($user, 'mymodule', $object->id, '', '', 'fk_soc', 'rowid', $isdraft);
  85. $permissionnote = $user->admin; // Used by the include of actions_setnotes.inc.php
  86. $permissiondellink = $user->admin; // Used by the include of actions_dellink.inc.php
  87. $permissiontoadd = $user->admin; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
  88. $debuginfo = '';
  89. $error = 0;
  90. /*
  91. * Actions
  92. */
  93. $parameters = array();
  94. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  95. if ($reshook < 0) {
  96. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  97. }
  98. if (empty($reshook)) {
  99. $permissiontoadd = 1;
  100. $permissiontodelete = 1;
  101. if (empty($backtopage)) {
  102. $backtopage = DOL_URL_ROOT.'/admin/emailcollector_card.php?id='.($id > 0 ? $id : '__ID__');
  103. }
  104. $backurlforlist = DOL_URL_ROOT.'/admin/emailcollector_list.php';
  105. // Actions cancel, add, update, delete or clone
  106. include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
  107. // Actions when linking object each other
  108. include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php';
  109. // Actions when printing a doc from card
  110. include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
  111. }
  112. if (GETPOST('addfilter', 'alpha')) {
  113. $emailcollectorfilter = new EmailCollectorFilter($db);
  114. $emailcollectorfilter->type = GETPOST('filtertype', 'aZ09');
  115. $emailcollectorfilter->rulevalue = GETPOST('rulevalue', 'alpha');
  116. $emailcollectorfilter->fk_emailcollector = $object->id;
  117. $emailcollectorfilter->status = 1;
  118. $result = $emailcollectorfilter->create($user);
  119. if ($result > 0) {
  120. $object->fetchFilters();
  121. } else {
  122. setEventMessages($emailcollectorfilter->error, $emailcollectorfilter->errors, 'errors');
  123. }
  124. }
  125. if ($action == 'deletefilter') {
  126. $emailcollectorfilter = new EmailCollectorFilter($db);
  127. $emailcollectorfilter->fetch(GETPOST('filterid', 'int'));
  128. if ($emailcollectorfilter->id > 0) {
  129. $result = $emailcollectorfilter->delete($user);
  130. if ($result > 0) {
  131. $object->fetchFilters();
  132. } else {
  133. setEventMessages($emailcollectorfilter->error, $emailcollectorfilter->errors, 'errors');
  134. }
  135. }
  136. }
  137. if (GETPOST('addoperation', 'alpha')) {
  138. $emailcollectoroperation = new EmailCollectorAction($db);
  139. $emailcollectoroperation->type = GETPOST('operationtype', 'aZ09');
  140. $emailcollectoroperation->actionparam = GETPOST('operationparam', 'restricthtml');
  141. $emailcollectoroperation->fk_emailcollector = $object->id;
  142. $emailcollectoroperation->status = 1;
  143. $emailcollectoroperation->position = 50;
  144. if ($emailcollectoroperation->type == '-1') {
  145. $error++;
  146. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Operation")), null, 'errors');
  147. }
  148. if (in_array($emailcollectoroperation->type, array('loadthirdparty', 'loadandcreatethirdparty'))
  149. && empty($emailcollectoroperation->actionparam)) {
  150. $error++;
  151. setEventMessages($langs->trans("ErrorAParameterIsRequiredForThisOperation"), null, 'errors');
  152. }
  153. if (!$error) {
  154. $result = $emailcollectoroperation->create($user);
  155. if ($result > 0) {
  156. $object->fetchActions();
  157. } else {
  158. $error++;
  159. setEventMessages($emailcollectoroperation->error, $emailcollectoroperation->errors, 'errors');
  160. }
  161. }
  162. }
  163. if ($action == 'updateoperation') {
  164. $emailcollectoroperation = new EmailCollectorAction($db);
  165. $emailcollectoroperation->fetch(GETPOST('rowidoperation2', 'int'));
  166. $emailcollectoroperation->actionparam = GETPOST('operationparam2', 'alphawithlgt');
  167. if (in_array($emailcollectoroperation->type, array('loadthirdparty', 'loadandcreatethirdparty'))
  168. && empty($emailcollectoroperation->actionparam)) {
  169. $error++;
  170. setEventMessages($langs->trans("ErrorAParameterIsRequiredForThisOperation"), null, 'errors');
  171. }
  172. if (!$error) {
  173. $result = $emailcollectoroperation->update($user);
  174. if ($result > 0) {
  175. $object->fetchActions();
  176. } else {
  177. $error++;
  178. setEventMessages($emailcollectoroperation->error, $emailcollectoroperation->errors, 'errors');
  179. }
  180. }
  181. }
  182. if ($action == 'deleteoperation') {
  183. $emailcollectoroperation = new EmailCollectorAction($db);
  184. $emailcollectoroperation->fetch(GETPOST('operationid', 'int'));
  185. if ($emailcollectoroperation->id > 0) {
  186. $result = $emailcollectoroperation->delete($user);
  187. if ($result > 0) {
  188. $object->fetchActions();
  189. } else {
  190. setEventMessages($emailcollectoroperation->error, $emailcollectoroperation->errors, 'errors');
  191. }
  192. }
  193. }
  194. if ($action == 'collecttest') {
  195. dol_include_once('/emailcollector/class/emailcollector.class.php');
  196. $res = $object->doCollectOneCollector(1);
  197. if ($res > 0) {
  198. $debuginfo = $object->debuginfo;
  199. setEventMessages($object->lastresult, null, 'mesgs');
  200. } else {
  201. $debuginfo = $object->debuginfo;
  202. setEventMessages($object->error, $object->errors, 'errors');
  203. }
  204. $action = '';
  205. }
  206. if ($action == 'confirm_collect') {
  207. dol_include_once('/emailcollector/class/emailcollector.class.php');
  208. $res = $object->doCollectOneCollector(0);
  209. if ($res > 0) {
  210. $debuginfo = $object->debuginfo;
  211. setEventMessages($object->lastresult, null, 'mesgs');
  212. } else {
  213. $debuginfo = $object->debuginfo;
  214. setEventMessages($object->error, $object->errors, 'errors');
  215. }
  216. $action = '';
  217. }
  218. /*
  219. * View
  220. */
  221. $form = new Form($db);
  222. $formfile = new FormFile($db);
  223. $help_url = "EN:Module_EMail_Collector|FR:Module_Collecteur_de_courrier_électronique|ES:Module_EMail_Collector";
  224. llxHeader('', 'EmailCollector', $help_url);
  225. // Part to create
  226. if ($action == 'create') {
  227. print load_fiche_titre($langs->trans("NewEmailCollector", $langs->transnoentitiesnoconv("EmailCollector")));
  228. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  229. print '<input type="hidden" name="token" value="'.newToken().'">';
  230. print '<input type="hidden" name="action" value="add">';
  231. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  232. print dol_get_fiche_head(array(), '');
  233. print '<table class="border centpercent tableforfield">'."\n";
  234. //unset($fields[]);
  235. // Common attributes
  236. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_add.tpl.php';
  237. // Other attributes
  238. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
  239. print '</table>'."\n";
  240. print dol_get_fiche_end();
  241. print $form->buttonsSaveCancel("Create");
  242. print '</form>';
  243. }
  244. // Part to edit record
  245. if (($id || $ref) && $action == 'edit') {
  246. print load_fiche_titre($langs->trans("EmailCollector"));
  247. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  248. print '<input type="hidden" name="token" value="'.newToken().'">';
  249. print '<input type="hidden" name="action" value="update">';
  250. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  251. print '<input type="hidden" name="id" value="'.$object->id.'">';
  252. print dol_get_fiche_head();
  253. print '<table class="border centpercent tableforfield">'."\n";
  254. // Common attributes
  255. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_edit.tpl.php';
  256. // Other attributes
  257. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_edit.tpl.php';
  258. print '</table>';
  259. print dol_get_fiche_end();
  260. print $form->buttonsSaveCancel();
  261. print '</form>';
  262. }
  263. // Part to show record
  264. if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
  265. $res = $object->fetch_optionals();
  266. $object->fetchFilters();
  267. $object->fetchActions();
  268. $head = emailcollectorPrepareHead($object);
  269. print dol_get_fiche_head($head, 'card', $langs->trans("EmailCollector"), -1, 'email');
  270. $formconfirm = '';
  271. // Confirmation to delete
  272. if ($action == 'delete') {
  273. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteEmailCollector'), $langs->trans('ConfirmDeleteEmailCollector'), 'confirm_delete', '', 0, 1);
  274. }
  275. // Clone confirmation
  276. if ($action == 'clone') {
  277. // Create an array for form
  278. $formquestion = array();
  279. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneEmailCollector', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
  280. }
  281. // Confirmation of action process
  282. if ($action == 'collect') {
  283. $formquestion = array();
  284. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('EmailCollectorConfirmCollectTitle'), $langs->trans('EmailCollectorConfirmCollect'), 'confirm_collect', $formquestion, 0, 1, 220);
  285. }
  286. // Call Hook formConfirm
  287. $parameters = array('formConfirm' => $formconfirm);
  288. $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  289. if (empty($reshook)) {
  290. $formconfirm .= $hookmanager->resPrint;
  291. } elseif ($reshook > 0) {
  292. $formconfirm = $hookmanager->resPrint;
  293. }
  294. // Print form confirm
  295. print $formconfirm;
  296. // Object card
  297. // ------------------------------------------------------------
  298. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/emailcollector_list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  299. $morehtmlref = '<div class="refidno">';
  300. $morehtmlref .= '</div>';
  301. $morehtml = '';
  302. $sourcedir = $object->source_directory;
  303. $targetdir = ($object->target_directory ? $object->target_directory : ''); // Can be '[Gmail]/Trash' or 'mytag'
  304. $connection = null;
  305. $connectstringserver = '';
  306. $connectstringsource = '';
  307. $connectstringtarget = '';
  308. // Note: $object->host has been loaded by the fetch
  309. $connectstringserver = $object->getConnectStringIMAP();
  310. if ($action == 'scan') {
  311. if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) {
  312. require_once DOL_DOCUMENT_ROOT.'/includes/webklex/php-imap/vendor/autoload.php';
  313. if ($object->acces_type == 1) {
  314. // Mode OAUth2 with PHP-IMAP
  315. require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php';
  316. $supportedoauth2array = getSupportedOauth2Array();
  317. $keyforsupportedoauth2array = $object->oauth_service;
  318. if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
  319. $keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
  320. } else {
  321. $keyforprovider = '';
  322. }
  323. $keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
  324. $keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
  325. $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array]['name']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['name'].($keyforprovider ? '-'.$keyforprovider : ''));
  326. require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
  327. //$debugtext = "Host: ".$this->host."<br>Port: ".$this->port."<br>Login: ".$this->login."<br>Password: ".$this->password."<br>access type: ".$this->acces_type."<br>oauth service: ".$this->oauth_service."<br>Max email per collect: ".$this->maxemailpercollect;
  328. //dol_syslog($debugtext);
  329. $token = '';
  330. $storage = new DoliStorage($db, $conf, $keyforprovider);
  331. try {
  332. $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
  333. $expire = true;
  334. // Is token expired or will token expire in the next 30 seconds
  335. // if (is_object($tokenobj)) {
  336. // $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
  337. // }
  338. // Token expired so we refresh it
  339. if (is_object($tokenobj) && $expire) {
  340. $credentials = new Credentials(
  341. getDolGlobalString('OAUTH_'.$object->oauth_service.'_ID'),
  342. getDolGlobalString('OAUTH_'.$object->oauth_service.'_SECRET'),
  343. getDolGlobalString('OAUTH_'.$object->oauth_service.'_URLAUTHORIZE')
  344. );
  345. $serviceFactory = new \OAuth\ServiceFactory();
  346. $oauthname = explode('-', $OAUTH_SERVICENAME);
  347. // ex service is Google-Emails we need only the first part Google
  348. $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
  349. // We have to save the token because Google give it only once
  350. $refreshtoken = $tokenobj->getRefreshToken();
  351. //var_dump($tokenobj);
  352. try {
  353. $tokenobj = $apiService->refreshAccessToken($tokenobj);
  354. } catch (Exception $e) {
  355. throw new Exception("Failed to refresh access token: ".$e->getMessage());
  356. }
  357. $tokenobj->setRefreshToken($refreshtoken);
  358. $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
  359. }
  360. $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
  361. if (is_object($tokenobj)) {
  362. $token = $tokenobj->getAccessToken();
  363. } else {
  364. $error++;
  365. $morehtml .= "Token not found";
  366. }
  367. } catch (Exception $e) {
  368. $error++;
  369. $morehtml .= $e->getMessage();
  370. }
  371. if (empty($object->login)) {
  372. $error++;
  373. $morehtml .= 'Error: Login is empty. Must be email owner when using MAIN_IMAP_USE_PHPIMAP and OAuth.';
  374. }
  375. $cm = new ClientManager();
  376. $client = $cm->make([
  377. 'host' => $object->host,
  378. 'port' => $object->port,
  379. 'encryption' => 'ssl',
  380. 'validate_cert' => true,
  381. 'protocol' => 'imap',
  382. 'username' => $object->login,
  383. 'password' => $token,
  384. 'authentication' => "oauth",
  385. ]);
  386. } else {
  387. // Mode login/pass with PHP-IMAP
  388. $cm = new ClientManager();
  389. $client = $cm->make([
  390. 'host' => $object->host,
  391. 'port' => $object->port,
  392. 'encryption' => 'ssl',
  393. 'validate_cert' => true,
  394. 'protocol' => 'imap',
  395. 'username' => $object->login,
  396. 'password' => $object->password,
  397. 'authentication' => "login",
  398. ]);
  399. }
  400. if (!$error) {
  401. try {
  402. // To emulate the command connect, you can run
  403. // openssl s_client -crlf -connect outlook.office365.com:993
  404. // TAG1 AUTHENTICATE XOAUTH2 dXN...
  405. // TO Get debug log, you can set protected $debug = true; in Protocol.php file
  406. //
  407. // A MS bug make this not working !
  408. // See https://github.com/MicrosoftDocs/office-developer-exchange-docs/issues/100
  409. // See github.com/MicrosoftDocs/office-developer-exchange-docs/issues/87
  410. // See github.com/Webklex/php-imap/issues/81
  411. $client->connect();
  412. $f = $client->getFolders(false, $object->source_directory);
  413. $nbemail = $f[0]->examine()["exists"];
  414. $morehtml .= $nbemail;
  415. } catch (ConnectionFailedException $e) {
  416. $morehtml .= 'ConnectionFailedException '.$e->getMessage();
  417. }
  418. }
  419. } else {
  420. if (function_exists('imap_open')) {
  421. try {
  422. if ($sourcedir) {
  423. //$connectstringsource = $connectstringserver.imap_utf7_encode($sourcedir);
  424. $connectstringsource = $connectstringserver.$object->getEncodedUtf7($sourcedir);
  425. }
  426. if ($targetdir) {
  427. //$connectstringtarget = $connectstringserver.imap_utf7_encode($targetdir);
  428. $connectstringtarget = $connectstringserver.$object->getEncodedUtf7($targetdir);
  429. }
  430. $timeoutconnect = !getDolGlobalString('MAIN_USE_CONNECT_TIMEOUT') ? 5 : $conf->global->MAIN_USE_CONNECT_TIMEOUT;
  431. $timeoutread = !getDolGlobalString('MAIN_USE_RESPONSE_TIMEOUT') ? 20 : $conf->global->MAIN_USE_RESPONSE_TIMEOUT;
  432. dol_syslog("imap_open connectstring=".$connectstringsource." login=".$object->login." password=".$object->password." timeoutconnect=".$timeoutconnect." timeoutread=".$timeoutread);
  433. $result1 = imap_timeout(IMAP_OPENTIMEOUT, $timeoutconnect); // timeout seems ignored with ssl connect
  434. $result2 = imap_timeout(IMAP_READTIMEOUT, $timeoutread);
  435. $result3 = imap_timeout(IMAP_WRITETIMEOUT, 5);
  436. $result4 = imap_timeout(IMAP_CLOSETIMEOUT, 5);
  437. dol_syslog("result1=".$result1." result2=".$result2." result3=".$result3." result4=".$result4);
  438. $connection = imap_open($connectstringsource, $object->login, $object->password);
  439. //dol_syslog("end imap_open connection=".var_export($connection, true));
  440. } catch (Exception $e) {
  441. $morehtml .= $e->getMessage();
  442. }
  443. if (!$connection) {
  444. $morehtml .= 'Failed to open IMAP connection '.$connectstringsource;
  445. if (function_exists('imap_last_error')) {
  446. $morehtml .= '<br>'.imap_last_error();
  447. }
  448. dol_syslog("Error ".$morehtml, LOG_WARNING);
  449. //var_dump(imap_errors())
  450. } else {
  451. dol_syslog("Imap connected. Now we call imap_num_msg()");
  452. $morehtml .= imap_num_msg($connection);
  453. }
  454. if ($connection) {
  455. dol_syslog("Imap close");
  456. imap_close($connection);
  457. }
  458. } else {
  459. $morehtml .= 'IMAP functions not available on your PHP. ';
  460. }
  461. }
  462. }
  463. $morehtml = $form->textwithpicto($langs->trans("NbOfEmailsInInbox"), 'Connect string = '.$connectstringserver.'<br>Option MAIN_IMAP_USE_PHPIMAP = '.getDolGlobalInt('MAIN_IMAP_USE_PHPIMAP')).': '.($morehtml !== '' ? $morehtml : '?');
  464. $morehtml .= '<a class="flat paddingleft marginleftonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=scan&token='.newToken().'">'.img_picto('', 'refresh', 'class="paddingrightonly"').$langs->trans("Refresh").'</a>';
  465. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref.'<div class="refidno">'.$morehtml.'</div>', '', 0, '', '', 0, '');
  466. print '<div class="fichecenter">';
  467. print '<div class="fichehalfleft">';
  468. print '<div class="underbanner clearboth"></div>';
  469. print '<table class="border centpercent tableforfield">'."\n";
  470. // Common attributes
  471. //$keyforbreak='fieldkeytoswithonsecondcolumn';
  472. include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
  473. // Other attributes
  474. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  475. print '</table>';
  476. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  477. print '<input type="hidden" name="token" value="'.newToken().'">';
  478. print '<input type="hidden" name="action" value="updatefiltersactions">';
  479. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  480. print '<input type="hidden" name="id" value="'.$object->id.'">';
  481. // Filters
  482. print '<div class="div-table-responsive-no-min">';
  483. print '<table id="tablelineoffilters" class="noborder nobordertop noshadow">';
  484. print '<tr class="liste_titre nodrag nodrop">';
  485. print '<td>'.img_picto('', 'filter', 'class="pictofixedwidth opacitymedium"').$form->textwithpicto($langs->trans("Filters"), $langs->trans("EmailCollectorFilterDesc")).'</td><td></td><td></td>';
  486. print '</tr>';
  487. // Add filter
  488. print '<tr class="oddeven nodrag nodrop">';
  489. print '<td>';
  490. $arrayoftypes = array(
  491. 'from'=>array('label'=>'MailFrom', 'data-placeholder'=>$langs->trans('SearchString')),
  492. 'to'=>array('label'=>'MailTo', 'data-placeholder'=>$langs->trans('SearchString')),
  493. 'cc'=>array('label'=>'Cc', 'data-placeholder'=>$langs->trans('SearchString')),
  494. 'bcc'=>array('label'=>'Bcc', 'data-placeholder'=>$langs->trans('SearchString')),
  495. 'replyto'=>array('label'=>'ReplyTo', 'data-placeholder'=>$langs->trans('SearchString')),
  496. 'subject'=>array('label'=>'Subject', 'data-placeholder'=>$langs->trans('SearchString')),
  497. 'body'=>array('label'=>'Body', 'data-placeholder'=>$langs->trans('SearchString')),
  498. // disabled because PHP imap_search is not compatible IMAPv4, only IMAPv2
  499. //'header'=>array('label'=>'Header', 'data-placeholder'=>'HeaderKey SearchString'), // HEADER key value
  500. //'X1'=>'---',
  501. 'X2'=>'---',
  502. 'seen'=>array('label'=>'AlreadyRead', 'data-noparam'=>1),
  503. 'unseen'=>array('label'=>'NotRead', 'data-noparam'=>1),
  504. 'unanswered'=>array('label'=>'Unanswered', 'data-noparam'=>1),
  505. 'answered'=>array('label'=>'Answered', 'data-noparam'=>1),
  506. 'smaller'=>array('label'=>$langs->trans("Size").' ('.$langs->trans("SmallerThan").")", 'data-placeholder'=>$langs->trans('NumberOfBytes')),
  507. 'larger'=>array('label'=>$langs->trans("Size").' ('.$langs->trans("LargerThan").")", 'data-placeholder'=>$langs->trans('NumberOfBytes')),
  508. 'X3'=>'---',
  509. 'withtrackingid'=>array('label'=>'WithDolTrackingID', 'data-noparam'=>1),
  510. 'withouttrackingid'=>array('label'=>'WithoutDolTrackingID', 'data-noparam'=>1),
  511. 'withtrackingidinmsgid'=>array('label'=>'WithDolTrackingIDInMsgId', 'data-noparam'=>1),
  512. 'withouttrackingidinmsgid'=>array('label'=>'WithoutDolTrackingIDInMsgId', 'data-noparam'=>1),
  513. 'X4'=>'---',
  514. 'isnotanswer'=>array('label'=>'IsNotAnAnswer', 'data-noparam'=>1),
  515. 'isanswer'=>array('label'=>'IsAnAnswer', 'data-noparam'=>1)
  516. );
  517. print $form->selectarray('filtertype', $arrayoftypes, '', 1, 0, 0, '', 1, 0, 0, '', 'maxwidth300', 1, '', 2);
  518. print "\n";
  519. print '<script>';
  520. print 'jQuery("#filtertype").change(function() {
  521. console.log("We change a filter");
  522. if (jQuery("#filtertype option:selected").attr("data-noparam")) {
  523. jQuery("#rulevalue").attr("placeholder", "");
  524. jQuery("#rulevalue").text("");
  525. jQuery("#rulevalue").prop("disabled", true);
  526. jQuery("#rulevaluehelp").addClass("unvisible");
  527. } else {
  528. jQuery("#rulevalue").prop("disabled", false);
  529. jQuery("#rulevaluehelp").removeClass("unvisible");
  530. }
  531. jQuery("#rulevalue").attr("placeholder", (jQuery("#filtertype option:selected").attr("data-placeholder")));
  532. ';
  533. /*$noparam = array();
  534. foreach ($arrayoftypes as $key => $value)
  535. {
  536. if ($value['noparam']) $noparam[] = $key;
  537. }*/
  538. print '})';
  539. print '</script>'."\n";
  540. print '</td><td class="nowraponall">';
  541. print '<div class="nowraponall">';
  542. print '<input type="text" name="rulevalue" id="rulevalue" class="inline-block valignmiddle">';
  543. print '<div class="inline-block valignmiddle unvisible" id="rulevaluehelp">';
  544. print img_warning($langs->trans("FilterSearchImapHelp"), '', 'pictowarning classfortooltip');
  545. print '</div>';
  546. print '</div>';
  547. print '</td>';
  548. print '<td class="right"><input type="submit" name="addfilter" id="addfilter" class="flat button smallpaddingimp" value="'.$langs->trans("Add").'"></td>';
  549. print '</tr>';
  550. // List filters
  551. foreach ($object->filters as $rulefilter) {
  552. $rulefilterobj = new EmailCollectorFilter($db);
  553. $rulefilterobj->fetch($rulefilter['id']);
  554. print '<tr class="oddeven">';
  555. print '<td title="'.dol_escape_htmltag($langs->trans("Filter").': '.$rulefilter['type']).'">';
  556. print $langs->trans($arrayoftypes[$rulefilter['type']]['label']);
  557. print '</td>';
  558. print '<td>'.$rulefilter['rulevalue'].'</td>';
  559. print '<td class="right">';
  560. print ' <a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=deletefilter&token='.urlencode(newToken()).'&filterid='.$rulefilter['id'].'">'.img_delete().'</a>';
  561. print '</td>';
  562. print '</tr>';
  563. }
  564. print '</tr>';
  565. print '</table>';
  566. print '</div>';
  567. print '<div class="clearboth"></div><br><br>';
  568. // Operations
  569. print '<div class="div-table-responsive-no-min">';
  570. print '<table id="tablelines" class="noborder noshadow">';
  571. print '<tr class="liste_titre nodrag nodrop">';
  572. print '<td>'.img_picto('', 'technic', 'class="pictofixedwidth"').$form->textwithpicto($langs->trans("EmailcollectorOperations"), $langs->trans("EmailcollectorOperationsDesc")).'</td>';
  573. print '<td>';
  574. $htmltext = $langs->transnoentitiesnoconv("OperationParamDesc");
  575. print $form->textwithpicto($langs->trans("Parameters"), $htmltext, 1, 'help', '', 0, 2, 'operationparamtt');
  576. print '</td>';
  577. print '<td></td>';
  578. print '<td></td>';
  579. print '</tr>';
  580. $arrayoftypes = array(
  581. 'loadthirdparty' => $langs->trans('LoadThirdPartyFromName', $langs->transnoentities("ThirdPartyName").'/'.$langs->transnoentities("AliasNameShort").'/'.$langs->transnoentities("Email").'/'.$langs->transnoentities("ID")),
  582. 'loadandcreatethirdparty' => $langs->trans('LoadThirdPartyFromNameOrCreate', $langs->transnoentities("ThirdPartyName").'/'.$langs->transnoentities("AliasNameShort").'/'.$langs->transnoentities("Email").'/'.$langs->transnoentities("ID")),
  583. 'recordjoinpiece' => 'AttachJoinedDocumentsToObject',
  584. 'recordevent' => 'RecordEvent'
  585. );
  586. $arrayoftypesnocondition = $arrayoftypes;
  587. if (isModEnabled('project')) {
  588. $arrayoftypes['project'] = 'CreateLeadAndThirdParty';
  589. }
  590. $arrayoftypesnocondition['project'] = 'CreateLeadAndThirdParty';
  591. if (isModEnabled('ticket')) {
  592. $arrayoftypes['ticket'] = 'CreateTicketAndThirdParty';
  593. }
  594. $arrayoftypesnocondition['ticket'] = 'CreateTicketAndThirdParty';
  595. if (isModEnabled('recruitment')) {
  596. $arrayoftypes['candidature'] = 'CreateCandidature';
  597. }
  598. $arrayoftypesnocondition['candidature'] = 'CreateCandidature';
  599. // support hook for add action
  600. $parameters = array('arrayoftypes' => $arrayoftypes);
  601. $res = $hookmanager->executeHooks('addMoreActionsEmailCollector', $parameters, $object, $action);
  602. if ($res) {
  603. $arrayoftypes = $hookmanager->resArray;
  604. } else {
  605. foreach ($hookmanager->resArray as $k => $desc) {
  606. $arrayoftypes[$k] = $desc;
  607. }
  608. }
  609. // Add operation
  610. print '<tr class="oddeven nodrag nodrop">';
  611. print '<td>';
  612. print $form->selectarray('operationtype', $arrayoftypes, '', 1, 0, 0, '', 1, 0, 0, '', 'minwidth150 maxwidth300', 1);
  613. print '</td><td>';
  614. print '<textarea class="centpercent" name="operationparam" rows="3"></textarea>';
  615. print '</td>';
  616. print '<td>';
  617. print '</td>';
  618. print '<td class="right"><input type="submit" name="addoperation" id="addoperation" class="flat button smallpaddingimp" value="'.$langs->trans("Add").'"></td>';
  619. print '</tr>';
  620. // List operations
  621. $nboflines = count($object->actions);
  622. $table_element_line = 'emailcollector_emailcollectoraction';
  623. $fk_element = 'position';
  624. $i = 0;
  625. foreach ($object->actions as $ruleaction) {
  626. $ruleactionobj = new EmailCollectorAction($db);
  627. $ruleactionobj->fetch($ruleaction['id']);
  628. print '<tr class="drag drop oddeven" id="row-'.$ruleaction['id'].'">';
  629. print '<td title="'.dol_escape_htmltag($langs->trans("Operation").': '.$ruleaction['type']).'">';
  630. print '<!-- type of action: '.$ruleaction['type'].' -->';
  631. if (array_key_exists($ruleaction['type'], $arrayoftypes)) {
  632. print $langs->trans($arrayoftypes[$ruleaction['type']]);
  633. } else {
  634. if (array_key_exists($ruleaction['type'], $arrayoftypesnocondition)) {
  635. print '<span class="opacitymedium">'.$langs->trans($arrayoftypesnocondition[$ruleaction['type']]).' - '.$langs->trans("Disabled").'</span>';
  636. }
  637. }
  638. if (in_array($ruleaction['type'], array('recordevent'))) {
  639. print $form->textwithpicto('', $langs->transnoentitiesnoconv('IfTrackingIDFoundEventWillBeLinked'));
  640. } elseif (in_array($ruleaction['type'], array('loadthirdparty', 'loadandcreatethirdparty'))) {
  641. print $form->textwithpicto('', $langs->transnoentitiesnoconv('EmailCollectorLoadThirdPartyHelp'));
  642. }
  643. print '</td>';
  644. print '<td class="wordbreak minwidth300 small">';
  645. if ($action == 'editoperation' && $ruleaction['id'] == $operationid) {
  646. //print '<input type="text" class="quatrevingtquinzepercent" name="operationparam2" value="'.dol_escape_htmltag($ruleaction['actionparam']).'"><br>';
  647. print '<textarea class="centpercent" name="operationparam2" rows="3">';
  648. print dol_escape_htmltag($ruleaction['actionparam'], 0, 1);
  649. print '</textarea>';
  650. print '<input type="hidden" name="rowidoperation2" value="'.$ruleaction['id'].'">';
  651. print '<input type="submit" class="button small button-save" name="saveoperation2" value="'.$langs->trans("Save").'">';
  652. print '<input type="submit" class="button small button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
  653. } else {
  654. print dol_nl2br(dol_escape_htmltag($ruleaction['actionparam'], 0, 1));
  655. }
  656. print '</td>';
  657. // Move up/down
  658. print '<td class="center linecolmove tdlineupdown">';
  659. if ($i > 0) {
  660. print '<a class="lineupdown" href="'.$_SERVER['PHP_SELF'].'?action=up&amp;rowid='.$ruleaction['id'].'">'.img_up('default', 0, 'imgupforline').'</a>';
  661. }
  662. if ($i < count($object->actions) - 1) {
  663. print '<a class="lineupdown" href="'.$_SERVER['PHP_SELF'].'?action=down&amp;rowid='.$ruleaction['id'].'">'.img_down('default', 0, 'imgdownforline').'</a>';
  664. }
  665. print '</td>';
  666. // Delete
  667. print '<td class="right nowraponall">';
  668. print '<a class="editfielda marginrightonly" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=editoperation&token='.newToken().'&operationid='.$ruleaction['id'].'">'.img_edit().'</a>';
  669. print ' <a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=deleteoperation&token='.newToken().'&operationid='.$ruleaction['id'].'">'.img_delete().'</a>';
  670. print '</td>';
  671. print '</tr>';
  672. $i++;
  673. }
  674. print '</tr>';
  675. print '</table>';
  676. print '</div>';
  677. if (!empty($conf->use_javascript_ajax)) {
  678. $urltorefreshaftermove = DOL_URL_ROOT.'/admin/emailcollector_card.php?id='.$id;
  679. include DOL_DOCUMENT_ROOT.'/core/tpl/ajaxrow.tpl.php';
  680. }
  681. print '</form>';
  682. print '</div>';
  683. print '</div>'; // End <div class="fichecenter">
  684. print '<div class="clearboth"></div><br>';
  685. print dol_get_fiche_end();
  686. // Buttons for actions
  687. if ($action != 'presend' && $action != 'editline') {
  688. print '<div class="tabsAction">'."\n";
  689. $parameters = array();
  690. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  691. if ($reshook < 0) {
  692. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  693. }
  694. if (empty($reshook)) {
  695. // Edit
  696. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=edit&token='.newToken().'">'.$langs->trans("Edit").'</a></div>';
  697. // Clone
  698. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=clone&token='.newToken().'&object=order">'.$langs->trans("ToClone").'</a></div>';
  699. // Collect now
  700. print '<div class="inline-block divButAction"><a class="butAction reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=collecttest&token='.newToken().'">'.$langs->trans("TestCollectNow").'</a></div>';
  701. if (count($object->actions) > 0) {
  702. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=collect&token='.newToken().'">'.$langs->trans("CollectNow").'</a></div>';
  703. } else {
  704. print '<div class="inline-block divButAction"><a class="butActionRefused" href="#" title="'.dol_escape_htmltag($langs->trans("NoOperations")).'">'.$langs->trans("CollectNow").'</a></div>';
  705. }
  706. print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.urlencode(newToken()).'">'.$langs->trans('Delete').'</a></div>';
  707. }
  708. print '</div>'."\n";
  709. }
  710. if (!empty($debuginfo)) {
  711. print info_admin($debuginfo);
  712. }
  713. }
  714. // End of page
  715. llxFooter();
  716. $db->close();