dolibarr 24.0.0-beta
externalModules.class.php
1<?php
2/*
3 * Copyright (C) 2025 Mohamed DAOUD <mdaoud@dolicloud.com>
4 * Copyright (C) 2025-2026 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modifyion 2.0 (the "License");
8 * it under the terms of the GNU General Public License as published bypliance with the License.
9 * the Free Software Foundation; either version 3 of the License, or
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
21include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
22
27{
31 public $no_page;
32
36 public $per_page;
40 public $categorie;
41
45 public $search;
46
47 // setups
55 public $file_source_url;
56
60 public $cache_file;
61
66 public $url;
70 public $shop_url; // the url of the shop
74 public $lang; // the integer representing the lang in the store
78 public $debug_api; // useful if no dialog
82 public $dolistore_api_url;
86 public $dolistore_api_key;
87
91 public $dolistoreApiStatus;
92
96 public $dolistoreApiError;
97
101 public $githubFileStatus;
102
106 public $githubFileError;
107
111 public $error;
112
116 public $numberOfProviders;
117
121 public $products;
122
126 public $numberTotalOfProducts;
127
131 public $numberTotalOfPages;
132
136 public $numberOfProducts;
137
143 public function __construct($debug = false)
144 {
145 global $langs;
146
147 $this->debug_api = $debug;
148
149 $this->url = DOL_URL_ROOT.'/admin/modules.php?mode=marketplace';
150
151 // For dolistore modules
152 $this->dolistore_api_url = getDolGlobalString('MAIN_MODULE_DOLISTORE_API_SRV', 'https://www.dolistore.com/api/'); // 'https://www.dolistore.com/api/', 'https://admin2.dolibarr.org/api/index.php/marketplace/'
153 $this->dolistore_api_key = getDolGlobalString('MAIN_MODULE_DOLISTORE_API_KEY', 'dolistorepublicapi');
154 $this->shop_url = getDolGlobalString('MAIN_MODULE_DOLISTORE_SHOP_URL', 'https://www.dolistore.com');
155
156 // For community modules
157 $this->file_source_url = "https://raw.githubusercontent.com/Dolibarr/dolibarr-community-modules/refs/heads/main/index.yaml";
158 $this->cache_file = DOL_DATA_ROOT.'/admin/temp/remote_github_modules_file.yaml';
159
160 $lang = $langs->defaultlang;
161 $lang_array = array('en_US', 'fr_FR', 'es_ES', 'it_IT', 'de_DE');
162 if (!in_array($lang, $lang_array)) {
163 $lang = 'en_US';
164 }
165 $this->lang = $lang;
166 }
167
174 public function loadRemoteSources($debug = false)
175 {
176 // Check access to Community repo
177 if (getDolGlobalString('MAIN_ENABLE_EXTERNALMODULES_COMMUNITY')) {
178 $cachedelayforgithubrepo = getDolGlobalInt('MAIN_REMOTE_GITHUBREPO_CACHE_DELAY', 86400);
179
180 $this->getRemoteYamlFile($this->file_source_url, $cachedelayforgithubrepo);
181
182 $this->githubFileError = $this->error;
183 $this->githubFileStatus = dol_is_file($this->cache_file) ? 1 : 0;
184 }
185
186 // Check access to Dolistore API /api/categories -> /api/index.php/marketplace/categories
187 if (getDolGlobalString('MAIN_ENABLE_EXTERNALMODULES_DOLISTORE')) {
188 $this->dolistoreApiStatus = $this->checkApiStatus();
189 }
190
191 // Count the number of online providers
192 $this->numberOfProviders = $this->dolistoreApiStatus + $this->githubFileStatus;
193 }
194
202 public function callApi($resource, $options = false)
203 {
204 // If no dolistore_api_key is set, we can't access the API
205 if (empty($this->dolistore_api_key) || empty($this->dolistore_api_url)) {
206 return array('status_code' => 0, 'response' => null);
207 }
208
209 // Add basic auth if needed
210 $basicAuthLogin = getDolGlobalString('MAIN_MODULE_DOLISTORE_BASIC_LOGIN');
211 $basicAuthPassword = getDolGlobalString('MAIN_MODULE_DOLISTORE_BASIC_PASSWORD');
212
213 $httpheader = array('DOLAPIKEY: '.$this->dolistore_api_key);
214 if ($basicAuthLogin) {
215 $httpheader[] = 'Authorization: Basic '.base64_encode($basicAuthLogin.':'.$basicAuthPassword);
216 }
217
218 $url = $this->dolistore_api_url . (preg_match('/\/$/', $this->dolistore_api_url) ? '' : '/') . $resource;
219
220 $options['apikey'] = $this->dolistore_api_key;
221
222 if ($options) {
223 $url .= '?' . http_build_query($options);
224 }
225
226 $response = getURLContent($url, 'GET', '', 1, $httpheader, array('https'), 0, -1, 5, 5);
227
228 $status_code = $response['http_code'];
229 $body = 'Error';
230
231 if ($status_code == 200) {
232 $body = $response['content'];
233 $body = json_decode($body, true);
234 $returnarray = array(
235 'status_code' => $status_code,
236 'response' => $body
237 );
238 } else {
239 $returnarray = array(
240 'status_code' => $status_code,
241 'response' => $body
242 );
243 if (!empty($response['curl_error_no'])) {
244 $returnarray['curl_error_no'] = $response['curl_error_no'];
245 }
246 if (!empty($response['curl_error_msg'])) {
247 $returnarray['curl_error_msg'] = $response['curl_error_msg'];
248 }
249 }
250
251 return $returnarray;
252 }
253
260 public function fetchModulesFromFile($options = array())
261 {
262 $modules = array();
263
264 if (!empty($this->cache_file) && file_exists($this->cache_file)) {
265 dol_syslog(__METHOD__ . " - Loading cache file: " . $this->cache_file, LOG_DEBUG);
266
267 $content = file_get_contents($this->cache_file);
268 if ($content !== false) {
269 $modules = $this->readYaml($content);
270 } else {
271 dol_syslog(__METHOD__ . " - Error reading cache file", LOG_ERR);
272 }
273 }
274
275 return $modules;
276 }
277
284 public function getCategories($active = 0)
285 {
286 $organized_tree = array();
287 $html = '';
288
289 $data = [
290 'lang' => $this->lang
291 ];
292
293 $current = $active;
294
295 $resCategories = $this->callApi('categories', $data);
296 if (isset($resCategories['response']) && is_array($resCategories['response'])) {
297 $organized_tree = $resCategories['response'];
298 } else {
299 return $html ;
300 }
301
302 $html = '';
303 foreach ($organized_tree as $key => $value) {
304 if ($value['label'] != "Versions" && $value['label'] != "Specials") {
305 $html .= '<li' . ($current == $value['rowid'] ? ' class="active"' : '') . '>';
306 $html .= '<a href="?mode=marketplace&categorie=' . $value['rowid'] . '">' . $value['label'] . '</a>';
307 if (isset($value['children'])) {
308 $html .= '<ul>';
309 usort($value['children'], $this->buildSorter('position'));
310 foreach ($value['children'] as $key_children => $value_children) {
311 $html .= '<li' . ($current == $value_children['rowid'] ? ' class="active"' : '') . '>';
312 $html .= '<a href="?mode=marketplace&categorie=' . $value_children['rowid'] . '" title="' . dol_escape_htmltag(strip_tags($value_children['description'])) . '">' . $value_children['label'] . '</a>';
313 $html .= '</li>';
314 }
315 $html .= '</ul>';
316 }
317 $html .= '</li>';
318 }
319 }
320 return $html;
321 }
322
329 public function getProducts($options)
330 {
331 global $langs;
332
333 $langs->load("products");
334
335 $html = "";
336 $last_month = dol_now() - (30 * 24 * 60 * 60);
337 $dolibarrversiontouse = DOL_VERSION; // full string with version
338
339 $this->products = array();
340
341 $this->categorie = $options['categorie'] ?? 0;
342 $this->per_page = $options['per_page'] ?? 11;
343 $this->no_page = $options['no_page'] ?? 1;
344 $this->search = $options['search'] ?? '';
345
346 $this->per_page = 11; // We fix number of products per page to 11
347
348 // Length of $search must be at least 2 characters
349 if (!empty($this->search) && strlen(str_replace(' ', '', (string) $this->search)) < 2) {
350 $html .= '<tr class=""><td colspan="3" class="center">';
351 $html .= '<br><br>';
352 $html .= $langs->trans("SearchStringMinLength").'...';
353 $html .= '<br><br>';
354 $html .= '</td></tr>';
355 return $html;
356 }
357
358 $data = [
359 'categorieid' => $this->categorie,
360 'limit' => $this->per_page,
361 'page' => $this->no_page,
362 'search' => $this->search,
363 'lang' => $this->lang
364 ];
365
366
367 $this->numberTotalOfProducts = 0;
368
369 // Special case of category goodies
370 if ($this->categorie == 87) {
371 $html = '<div class="shop-container">
372 <div class="shop-image">
373 <a href="https://merch.dolibarr.org/" target="_blank">
374 <img src="https://www.dolistore.com/medias/image/marketplace/img/goodies-shop.jpg" width="50%" alt="DoliStore Merch and Gifts" />
375 <div class="shop-overlay">
376 <button target="new" class="shop-button">'.$langs->trans("GoodiesButtonTitle").' <i class="icon-chevron-right"></i></button>
377 </div>
378 </a>
379 </div>
380 </div>';
381
382 return $html;
383 }
384
385 // Fetch the products from Dolistore source
386
387 $dolistoreProducts = array();
388 $dolistoreProductsTotal = 0;
389 if ($this->dolistoreApiStatus > 0 && getDolGlobalInt('MAIN_ENABLE_EXTERNALMODULES_DOLISTORE')) {
390 $getDolistoreProducts = $this->callApi('products', $data);
391
392 if (!isset($getDolistoreProducts['response']) || !is_array($getDolistoreProducts['response']) || ($getDolistoreProducts['status_code'] != 200 && $getDolistoreProducts['status_code'] != 201)) {
393 $dolistoreProducts = array();
394 $dolistoreProductsTotal = 0;
395 } else {
396 $dolistoreProducts = $this->adaptData($getDolistoreProducts['response']['products'], 'dolistore');
397 $dolistoreProductsTotal = (int) $getDolistoreProducts['response']['total'];
398 $this->numberTotalOfProducts += $dolistoreProductsTotal;
399 }
400 }
401
402 // Fetch the products from the github repo
403
404 $fileProducts = array();
405 $fileProductsTotal = 0;
406 if (!empty($this->githubFileStatus) && getDolGlobalInt('MAIN_ENABLE_EXTERNALMODULES_COMMUNITY')) {
407 $fileProducts = $this->fetchModulesFromFile($data); // Return an array with all modules from the cache filecontent in $data
408
409 $fileProducts = $this->adaptData($fileProducts, 'githubcommunity');
410
411 $fileProducts = $this->applyFilters($fileProducts, $data);
412
413 $fileProductsTotal = $fileProducts['total'];
414
415 $this->numberTotalOfProducts += $fileProductsTotal;
416
417 $fileProducts = $fileProducts['data'];
418 }
419
420 // Number of pages
421 $this->numberTotalOfPages = (int) ceil(max($fileProductsTotal / $this->per_page, $dolistoreProductsTotal / $this->per_page));
422
423 // Merge both sources (github community modules have priority on dolistore).
424 $this->products = $dolistoreProducts;
425 foreach ($fileProducts as $fileProduct) {
426 $id = $fileProduct['id'];
427 if ($id > 0) {
428 if (empty($this->products[$id])) { // Not already present in array
429 array_unshift($this->products, $fileProduct);
430 } else {
431 $this->products[$id] = $fileProduct;
432 $this->products[$id]['category'] = $fileProduct['category'];
433 }
434 } else {
435 array_unshift($this->products, $fileProduct);
436 }
437 }
438
439
440 $i = 0;
441 foreach ($this->products as $product) {
442 $i++;
443
444 // check new product ?
445 $newapp = '';
446 if ($last_month < strtotime($product['datec']) && $product["status"] != 'soon' && $product["status"] != 'development' && $product["status"] != 'experimental') {
447 $newapp .= '<span class="newApp" title="'.$product['tms'].'">'.$langs->trans('New').'</span> ';
448 }
449
450 // check updated ?
451 if ($newapp == '' && $last_month < strtotime($product['tms']) && $product["status"] != 'soon' && $product["status"] != 'development' && $product["status"] != 'experimental') {
452 $newapp .= '<span class="updatedApp" title="'.$product['tms'].'">'.$langs->trans('UpdatedRecently').'</span> ';
453 }
454
455 // add image or default ?
456 if ($product["cover_photo_url"] != '' && $product["cover_photo_url"] != '#') {
457 $images = '<a href="'.$product["cover_photo_url"].'" class="documentpreview" target="_blank" rel="noopener noreferrer" mime="image/png" title="'.dol_escape_htmltag($product["label"].', '.$langs->trans('Version').' '.$product["module_version"]).'">';
458 $images .= '<img class="imgstore" src="'.$product["cover_photo_url"].'" alt="" /></a>';
459 } else {
460 $images = '<img class="imgstore" src="'.DOL_URL_ROOT.'/public/theme/common/nophoto.png" />';
461 }
462
463 // Set and check version
464 $version = '';
465 $compatible = '';
466 if ($product["status"] == 'soon' || $product["status"] == 'development' || $product["status"] == 'experimental') {
467 $version = '<span class="warning">'.$langs->trans("NotYetAvailable").' - '.$langs->trans("StillInDevelopment").'</span>';
468 $compatible = 'NotCompatible';
469 } elseif ($this->versionCompare($product["dolibarr_min"], $dolibarrversiontouse) <= 0) {
470 if (!empty($product["dolibarr_max"]) && $product["dolibarr_max"] != 'auto' && $product["dolibarr_max"] != 'unknown' && $this->versionCompare($product["dolibarr_max"], $dolibarrversiontouse) >= 0) {
471 // Compatible
472 $version = '<span class="compatible hideonsmartphone">'.$langs->trans(
473 'CompatibleUpTo',
474 $dolibarrversiontouse,
475 $product["dolibarr_min"],
476 $product["dolibarr_max"]
477 ).'</span>';
478 $compatible = '';
479 } else {
480 // Never compatible, module expired
481 $version = '<span class="warning">'.$langs->trans(
482 'NotCompatible',
483 $dolibarrversiontouse,
484 $product["dolibarr_min"],
485 $product["dolibarr_max"]
486 ).'</span>';
487 $compatible = 'NotCompatible';
488 }
489 } else {
490 if ($product["dolibarr_min"] == 'auto' || $product["dolibarr_min"] != 'unknown') {
491 // Never compatible, module expired
492 $version = '<span class="warning">'.$langs->trans(
493 'NotCompatible',
494 $dolibarrversiontouse,
495 $product["dolibarr_min"],
496 $product["dolibarr_max"]
497 ).'</span>';
498 $compatible = 'NotCompatible';
499 } else {
500 // Need update
501 $version = '<span class="compatibleafterupdate">'.$langs->trans(
502 'CompatibleAfterUpdate',
503 $dolibarrversiontouse,
504 $product["dolibarr_min"],
505 $product["dolibarr_max"]
506 ).'</span>';
507 $compatible = 'NotCompatible';
508 }
509 }
510
511 // free or pay ?
512 $install_link = '';
513 if (array_key_exists('price_ht', $product) && price2num($product["price_ht"]) > 0) {
514 $price = '<h3>'.price(price2num($product["price_ht"], 'MT'), 0, $langs, 1, -1, -1, 'EUR').' '.$langs->trans("HT").'</h3>';
515
516 $download_link = '<a class="paddingleft paddingright valignmiddle" target="_blank" title="'.$langs->trans("View").'" href="'.$this->shop_url.'/product.php?id='.((int) $product['id']).'">';
517 $download_link .= img_picto('', 'url', 'class="size2x paddingright"');
518 $download_link .= '</a>';
519 } else {
520 $download_link = '#';
521 if ($product['source'] === 'dolistore') { // 0 on dolistore may mean 0 or a complementary fee to subscribe
522 $urlview = $this->shop_url.'/product.php?id='.((int) $product["id"]);
523 $price = '<h3><a href="'.$urlview.'" target="_blank">'.$langs->trans('SeeOnDoliStore').'</a></h3>';
524 } elseif ($product['source'] === 'githubcommunity') {
525 if (array_key_exists('price_ht', $product) && empty($product['price_ht'])) {
526 if ($product['status'] == 'soon') {
527 $price = '<h3>'.$langs->trans('StillInDevelopment').'</h3>';
528 } else {
529 $price = '<h3>'.$langs->trans('Free').'</h3>';
530 }
531 } else {
532 if ($product["dolistore-download"]) {
533 $price = '<h3><a href="'.$product["dolistore-download"].'" target="_blank">'.$langs->trans('SeeOnDoliStore').'</a></h3>';
534 } else {
535 $price = '<h3>'.$langs->trans('Unknown').'</h3>';
536 }
537 }
538 } else {
539 $price = '<h3>'.$langs->trans('Unknown').'</h3>';
540 }
541
542 if ($product['source'] === 'githubcommunity') {
543 $download_link = '<a class="paddingleft paddingright valignmiddle" target="_blank" title="'.$langs->trans("Sources").'" href="'.$product["link"].'">';
544 $download_link .= img_picto('', 'file-code', 'class="size2x paddingright colorgrey"');
545 $download_link .= '</a>';
546
547 $urlview = $product["dolistore-download"]; // View on Dolistore
548 if ($urlview) {
549 $download_link .= '<a class="paddingleft paddingright valignmiddle" target="_blank" title="'.$langs->trans("View").'" href="'.$urlview.'" rel="noopener noreferrer">';
550 $download_link .= img_picto('', 'url', 'class="size2x"');
551 $download_link .= '</a>';
552 }
553
554 if (!empty($product['direct-download']) && $product['direct-download'] == 'yes') {
555 $reg = array();
556 if (preg_match('/https:.*\?id=(\d+)$/', $urlview, $reg)) {
557 $urldownload = 'https://www.dolistore.com/_service_download.php?t=free&p='.$reg[1];
558 $download_link .= '<a class="paddingleft paddingright valignmiddle" target="_blank" title="'.$langs->trans("Download").'" href="'.$urldownload.'" rel="noopener noreferrer">';
559 $download_link .= img_picto('', 'download', 'class="size2x paddingright"');
560 //$download_link .= '<img width="32" src="'.DOL_URL_ROOT.'/admin/remotestore/img/download.png" />';
561 $download_link .= '</a>';
562 }
563 }
564 } elseif ($product['source'] === 'dolistore') {
565 $urlview = $this->shop_url.'/product.php?id='.((int) $product["id"]);
566 $urldownload = 'https://www.dolistore.com/_service_download.php?t=free&p=' . $product['id'];
567 $download_link = '<a class="paddingleft paddingright valignmiddle" target="_blank" title="'.$langs->trans("View").'" href="'.$urlview.'">';
568 $download_link .= img_picto('', 'url', 'class="size2x"');
569 $download_link .= '</a>';
570 $download_link .= '<a class="paddingleft paddingright" target="_blank" title="'.$langs->trans("Download").'" href="'.$urldownload.'" rel="noopener noreferrer">';
571 $download_link .= img_picto('', 'download', 'class="size2x paddingright"');
572 //$download_link .= '<img width="32" src="'.DOL_URL_ROOT.'/admin/remotestore/img/download.png" />';
573 $download_link .= '</a>';
574 }
575
576 // Direct install
577 if (($product['direct-download'] && $product['direct-download'] == 'yes') || $product['source'] === 'dolistore') {
578 $disableInstall = ($compatible === 'NotCompatible');
579 // $disableInstall = false; // TODO: remove this.
580 $disableInfo = $disableInstall ? dol_string_nohtmltag($version) : '';
581 $fields = ['action' => 'install', 'token' => newToken()];
582 foreach ($product as $key => $value) {
583 $fields['producttoinstall['.$key.']'] = $value;
584 }
585
586 $install_link = '<button class="valignmiddle ' . ($disableInstall ? 'butActionRefused' : 'butAction') . ' paddingleft paddingright"'
587 . ($disableInfo ? ' title="' . dol_escape_htmltag($disableInfo) . '"' : '')
588 . (!$disableInstall ? ' data-confirm' : '')
589 . (!$disableInstall ? ' data-fields="' . dol_escape_htmltag(json_encode($fields)) . '"' : '')
590 . (!$disableInstall ? ' data-url="' . dol_escape_htmltag($this->url) . '"' : '')
591 . (!$disableInstall ? ' data-confirm-title="' . dol_escape_htmltag($langs->trans("extModuleConfirmInstallTitle")) . '"' : '')
592 . (!$disableInstall ? ' data-confirm-text="' . dol_escape_htmltag($langs->transnoentities(
593 "extModuleConfirmInstallText",
594 $product['label'] ?? '',
595 $product['module_version'] ?? '',
596 $product['ref'] ?? '',
597 !empty($product['tms']) ? dol_print_date($product['tms'], '%d/%m/%Y') : ''
598 )) . '"' : '')
599 . '>' . $langs->trans("install") . '</button>';
600 }
601 }
602
603 // Output the line
604 $html .= '<tr class="'.(getDolOptimizeSmallScreen() ? 'app' : 'app app2').' oddeven nohover '.dol_escape_htmltag($compatible).'">';
605
606 // Logo
607 $html .= '<td class="center width150"><div class="newAppParent">';
608 $html .= $newapp.$images; // No dol_escape_htmltag, it is already escape html
609 $html .= '</div></td>';
610
611 // Description
612 $html .= '<td class="margeCote minwidth400imp"><h2 class="appTitle">';
613 $html .= dolPrintHTML(dol_string_nohtmltag(ucfirst($product["label"])));
614 if (!empty($product['author']) && $product['author'] != 'unkownauthor') {
615 $html .= '<span class="small"> &nbsp; - &nbsp; '.img_picto('', 'company', 'class="pictofixedwidth"');
616 if (!empty($product['author_url'])) {
617 $html .= '<a href="'.$product['author_url'].'" target="_blank">'.$product['author'].'</a>';
618 } else {
619 $html .= $product['author'];
620 }
621 $html .= '</span>';
622 }
623 $html .= '<br><span class="small">';
624 $html .= $version; // Version Dolibarr. No dol_escape_htmltag, it is already escape html
625 $html .= '</span>';
626 $html .= '</h2>';
627
628 $html .= '<small class="appDateCreation appRef"> ';
629 if (empty($product['tms'])) {
630 $html .= img_picto($langs->trans('DateCreation'), 'calendar', 'class="pictofixedwidth"').'<span class="opacitymedium"><span class="hideonsmartphone">'.$langs->trans("DateCreation").': </span>';
631 $html .= (!empty($product['datec']) ? dol_print_date(dol_stringtotime($product['datec']), 'day') : $langs->trans("Unknown")).'</span>';
632 } else {
633 $html .= img_picto($langs->trans('DateModification'), 'calendar', 'class="pictofixedwidth"').'<span class="opacitymedium">'.dol_print_date(dol_stringtotime($product['tms']), 'day').'</span>';
634 }
635 $html .= ' &nbsp; &nbsp; ';
636
637 $html .= '<div class="appSource inline-block valigntop">';
638 if ($product["source"] == 'dolistore') {
639 $html .= '<img border="0" title="'.dolPrintHTML($langs->trans('Source').": DoliStore").'" class="imgautosize valignmiddle inline-block pictofixedwidth" style="height: 14px" src="'.DOL_URL_ROOT.'/theme/dolistore_squarred.svg">';
640 } elseif ($product["source"] == 'githubcommunity') {
641 $html .= img_picto($langs->trans('Source').': GitHub community repo', 'group', 'class="pictofixedwidth valignmiddle"');
642 } else {
643 $html .= img_picto($langs->trans('Source').': '.$langs->trans('Other'), 'generic', 'class="pictofixedwidth"');
644 }
645 $html .= '</div>';
646
647 $html .= $langs->trans('Ref').' '.dolPrintHTML(preg_replace('/@.*$/', '', $product["ref"]));
648 $html .= '</small><br>';
649
650
651 $html .= '&nbsp;';
652 if (!empty($product['phpmin']) && $product['phpmin'] != 'unknown') {
653 $html .= ' <span class="badge-secondary small" style="padding: 3px; border-radius: 5px">PHP min '.$product['phpmin'].'</span>';
654 }
655 if (!empty($product['phpmax']) && $product['phpmax'] != 'unknown') {
656 $html .= ' <span class="badge-secondary small" style="padding: 3px; border-radius: 5px">PHP max '.$product['phpmax'].'</span>';
657 }
658 $html .= '<br>';
659
660 $html .= '<br>';
661 $html .= '<div class="storedesc">'.dolPrintHTML(dol_string_nohtmltag($product["description"])).'</div>';
662 $html .= '</td>';
663
665 $html .= '</tr><tr class="app2 oddeven nohover borderbottom '.dol_escape_htmltag($compatible).'">';
666 }
667
668 // Price - do not load if display none
669 $html .= '<td class="margeCote center amount'.(getDolOptimizeSmallScreen() ? ' left" colspan="2"' : '"').'>';
670 $html .= $price;
671 if (($product['direct-download'] && $product['direct-download'] == 'yes')
672 || ($product['source'] === 'dolistore' && empty((float) $product['price_ht']))) {
673 if ($install_link) {
674 $html .= $install_link;
675 }
676 }
677
679 $html .= '</td>';
680 $html .= '<td class="margeCote nowraponall">';
681 }
682
683 // Links
684 $html .= $download_link;
685 $html .= '</td>';
686
687 $html .= '</tr>';
688 }
689
690 if (empty($this->products)) {
691 $colspan = (getDolOptimizeSmallScreen() ? 1 : 3);
692 $langs->load("website");
693
694 $html .= '<tr class=""><td colspan="'.$colspan.'" class="center">';
695 $html .= '<br><br>';
696 $html .= $langs->trans("noResultsWereFound").'...';
697 $html .= '<br><br>';
698 $html .= '</td></tr>';
699 }
700
701 // JS for confirm install
702 $confirmLabel = dol_escape_js($langs->trans("install"));
703 $cancelLabel = dol_escape_js($langs->trans("Cancel"));
704 $html .= '<script>
705 $(document).on("click","[data-confirm]",function(){
706 var button = $(this);
707 var confirmTitle = button.data("confirm-title");
708 var confirmText = button.data("confirm-text");
709 var buttons = {};
710 buttons[button.data("confirm-label")||"' . $confirmLabel . '"] = function(){
711 var form = $("<form method=\'POST\' style=\'display:none\'>").attr("action", button.data("url"));
712 $.each(button.data("fields"), function(name, value){
713 form.append($("<input type=\'hidden\'>").attr("name", name).val(value));
714 });
715 $("body").append(form);
716 form.submit();
717 $(this).dialog("close");
718 };
719 buttons["' . $cancelLabel . '"] = function(){$(this).dialog("close");};
720 $("<div>").html(confirmText).dialog({
721 title: confirmTitle,
722 minWidth: 580,
723 modal: true,
724 buttons: buttons
725 });
726 });
727 </script>';
728
729 $this->numberOfProducts = count($this->products);
730
731 return $html;
732 }
733
740 public function buildSorter(string $key): Closure
741 {
742 return
748 function (array $a, array $b) use ($key) {
749 $valA = isset($a[$key]) && is_scalar($a[$key]) ? (string) $a[$key] : '';
750 $valB = isset($b[$key]) && is_scalar($b[$key]) ? (string) $b[$key] : '';
751
752 return strnatcmp($valA, $valB);
753 };
754 }
755
763 public function versionCompare($v1, $v2)
764 {
765 // Clean v1 and v2
766 $v1 = str_replace(array('v', 'V'), '', $v1);
767 $v2 = str_replace(array('v', 'V'), '', $v2);
768
769 $v1 = explode('.', $v1);
770 $v2 = explode('.', $v2);
771 $ret = 0;
772 $level = 0;
773 $count1 = count($v1);
774 $count2 = count($v2);
775 $maxcount = max($count1, $count2);
776 while ($level < $maxcount) {
777 $operande1 = isset($v1[$level]) ? $v1[$level] : 'x';
778 $operande2 = isset($v2[$level]) ? $v2[$level] : 'x';
779 $level++;
780 if (strtoupper($operande1) == 'X' || strtoupper($operande2) == 'X' || $operande1 == '*' || $operande2 == '*') {
781 break;
782 }
783 if ($operande1 < $operande2) {
784 $ret = -$level;
785 break;
786 }
787 if ($operande1 > $operande2) {
788 $ret = $level;
789 break;
790 }
791 }
792 //print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret.'<br>'."\n";
793 return $ret;
794 }
795
796 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
803 public function get_previous_link($text = '<<')
804 {
805 // phpcs:enable
806 return '<a href="'.$this->get_previous_url().'" class="button">'.dol_escape_htmltag($text).'</a>';
807 }
808
809 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
816 public function get_next_link($text = '>>')
817 {
818 // phpcs:enable
819 return '<a href="'.$this->get_next_url().'" class="button">'.dol_escape_htmltag($text).'</a>';
820 }
821
822 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
828 public function get_previous_url()
829 {
830 // phpcs:enable
831 $param_array = array();
832 if ($this->no_page > 1) {
833 $sub = 1;
834 } else {
835 $sub = 0;
836 }
837 if (!empty($this->search)) {
838 $param_array['search_keyword'] = $this->search;
839 }
840 $param_array['no_page'] = $this->no_page - $sub;
841 if ($this->categorie != 0) {
842 $param_array['categorie'] = $this->categorie;
843 }
844 $param = http_build_query($param_array);
845 return $this->url."&".$param;
846 }
847
848 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
854 public function get_next_url()
855 {
856 // phpcs:enable
857 $param_array = array();
858 if ($this->products !== null && count($this->products) < $this->per_page) {
859 $add = 0;
860 } else {
861 $add = 1;
862 }
863 if (!empty($this->search)) {
864 $param_array['search_keyword'] = $this->search;
865 }
866 $param_array['no_page'] = $this->no_page + $add;
867 if ($this->categorie != 0) {
868 $param_array['categorie'] = $this->categorie;
869 }
870 $param = http_build_query($param_array);
871 return $this->url."&".$param;
872 }
873
879 public function getPagination()
880 {
881
882 global $langs;
883
884 $page = $this->no_page;
885 $limit = $this->per_page;
886 $totalnboflines = $this->numberTotalOfProducts ?: 0;
887 $num = $this->numberOfProducts;
888
889 $html = "";
890
891 // Show navigation bar
892 $pagelist = '';
893 if ($page > 0 || $num > $limit) {
894 if ($totalnboflines) {
895 if ($limit > 0) {
896 $nbpages = $this->numberTotalOfPages;
897 } else {
898 $nbpages = 1;
899 }
900
901 // Show previous page
902 if ($page > 1) {
903 $pagelist .= '<li class="pagination paginationpage paginationpageleft"><a class="paginationprevious reposition" href="'.$this->get_previous_url().'"><i class="fa fa-chevron-left" title="'.dol_escape_htmltag($langs->trans("Previous")).'"></i></a></li>';
904 }
905
906 $pagelist .= '<li class="pagination">';
907 $pagelist .= '<label for="page_input">Page </label>';
908 if ($this->categorie != 0) {
909 $pagelist .= '<input type="hidden" name="categorie" value="' . $this->categorie . '">';
910 }
911 $pagelist .= '<input type="text" id="page_input" name="no_page" value="'.($page).'" min="1" max="'.$nbpages.'" class="width40 page_input right" oninput="if(this.value > '.$nbpages.') this.value='.$nbpages.'">';
912 $pagelist .= ' / '.$nbpages;
913 $pagelist .= '</li>';
914
915 // Show next page
916 if ($page < $nbpages) {
917 $pagelist .= '<li class="pagination paginationpage paginationpageright"><a class="paginationnext reposition" href="'.$this->get_next_url().'"><i class="fa fa-chevron-right" title="'.dol_escape_htmltag($langs->trans("Next")).'"></i></a></li>';
918 }
919 }
920 }
921
922 if ($limit || $pagelist) {
923 $html .= '<div class="pagination" style="padding: 7px;">';
924 $html .= '<ul>';
925 $html .= $pagelist;
926 $html .= '</ul>';
927 $html .= '</div>';
928 }
929
930 $html .= ajax_autoselect('.page_input');
931
932 return $html;
933 }
934
941 protected function checkStatusCode($request)
942 {
943 // Define error messages
944 $error_messages = [
945 204 => 'No content',
946 400 => 'Bad Request',
947 401 => 'Unauthorized',
948 404 => 'Not Found',
949 405 => 'Method Not Allowed',
950 500 => 'Internal Server Error',
951 ];
952
953 // If status code is 200 or 201, return an empty string
954 if ($request['status_code'] === 200 || $request['status_code'] === 201) {
955 return '';
956 }
957
958 // Get the predefined error message or use a default one
959 $error_message = $error_messages[$request['status_code']] ?? 'Unexpected HTTP status: ' . $request['status_code'];
960
961 // Append error details if available
962 if (!empty($request['response']) && isset($request['response']['errors']) && is_array($request['response']['errors'])) {
963 foreach ($request['response']['errors'] as $error) {
964 $error_message .= ' - (Code ' . $error['code'] . '): ' . $error['message'];
965 }
966 }
967
968 if (!empty($request['curl_error_msg'])) {
969 $error_message .= ' - ' . $request['curl_error_msg'];
970 }
971
972 // Return the formatted error message
973 return sprintf('This call to the API failed and returned an HTTP status of %d. That means: %s.', $request['status_code'], $error_message);
974 }
975
983 public function getRemoteYamlFile($file_source_url, $cache_time)
984 {
985 $yaml = '';
986 $cache_file = $this->cache_file;
987 $cache_folder = dirname($cache_file);
988
989 // Check if cache directory exists
990 if (!dol_is_dir($cache_folder)) {
991 dol_mkdir($cache_folder, DOL_DATA_ROOT);
992 }
993
994 if (!file_exists($cache_file) || filemtime($cache_file) < (dol_now() - $cache_time)) {
995 // We get remote url
996 $addheaders = array();
997 $result = getURLContent($file_source_url, 'GET', '', 1, $addheaders); // TODO Force timeout to 5 s on both connect and response.
998 if (!empty($result) && $result['http_code'] == 200) {
999 $yaml = $result['content'];
1000 $result = file_put_contents($cache_file, $yaml);
1001 if ($result === false) {
1002 $this->error = 'Failed to create cache file: ' . $cache_file;
1003 } else {
1004 dolChmod($cache_file);
1005 }
1006 }
1007 } else {
1008 $yaml = file_get_contents($cache_file);
1009 }
1010
1011 return $yaml;
1012 }
1013
1014
1021 public function readYaml($yaml)
1022 {
1023 $data = [];
1024 $currentPackage = null;
1025 $currentSection = null;
1026
1027 foreach (explode("\n", trim($yaml)) as $line) {
1028 $trimmedLine = trim($line);
1029
1030 // Ignore empty lines and comments
1031 if ($trimmedLine === '' || strpos($trimmedLine, '#') === 0) {
1032 continue;
1033 }
1034
1035 // Match a new package entry (e.g., "- modulename: 'helloasso'") - Found a break in file.
1036 $matches = array();
1037 if (preg_match('/^\s*-\s*modulename:\s*["\']?(.*?)["\']?$/', $trimmedLine, $matches)) {
1038 if ($currentPackage !== null) {
1039 // Add the package to $data
1040 if (!empty($currentPackage['status']) && in_array($currentPackage['status'], array('enabled', 'soon'))) {
1041 $data[] = $currentPackage;
1042 }
1043 }
1044 $currentPackage = ['modulename' => $matches[1]];
1045 $currentSection = null;
1046 continue;
1047 }
1048
1049 // If the key doesn't start with fr, en, es, it, de, treat it as a section
1050 if (!preg_match('/^\s*(fr|en|es|it|de):\s*["\']?(.*?)["\']?$/', $trimmedLine)) {
1051 $currentSection = null;
1052 }
1053
1054 // Match a top-level key-value pair (e.g., "author: 'Dolicloud'")
1055 if (preg_match('/^(\w[\w-]*):\s*["\']?(.*?)["\']?$/', $trimmedLine, $matches)) {
1056 if ($currentPackage !== null) {
1057 if ($currentSection) {
1058 // Store in the sub section (language into label or description for example)
1059 $currentPackage[$currentSection][$matches[1]] = $matches[2] === '' ? null : $matches[2];
1060 } else {
1061 // Store as a normal key-value pair
1062 $currentPackage[$matches[1]] = $matches[2] === '' ? null : $matches[2];
1063 }
1064 }
1065
1066 // Match a nested section (e.g., "label:")
1067 if (preg_match('/^\s*(label|description):\s*$/', $trimmedLine, $matches)) {
1068 $currentSection = $matches[1];
1069 $currentPackage[$currentSection] = []; // Initialize as an empty array for nested sections
1070 }
1071
1072 continue;
1073 }
1074 }
1075
1076 // Add the last package if available
1077 if ($currentPackage !== null) {
1078 if (!empty($currentPackage['status']) && in_array($currentPackage['status'], array('enabled', 'soon'))) {
1079 $data[] = $currentPackage;
1080 }
1081 }
1082
1083 return $data;
1084 }
1085
1093 public function adaptData($data, $source)
1094 {
1095 $adaptedData = [];
1096
1097 if (!is_array($data) || empty($data) || empty($source)) {
1098 return $adaptedData;
1099 }
1100
1101 if ($source === 'githubcommunity') {
1102 foreach ($data as $package) {
1103 if (empty($package['modulename'])) {
1104 continue;
1105 }
1106
1107 // Check if there is a known ID
1108 $reg = array();
1109 $id = 0;
1110 if (!empty($package['dolistore-download']) && preg_match('/www\.dolistore\.com\/product\.php\?id=(\d+)/', (string) $package['dolistore-download'], $reg)) {
1111 $id = $reg[1];
1112 }
1113
1114 $adaptedPackage = [
1115 'id' => $id,
1116 'ref' => str_replace(' ', '', $package['modulename'] . '-' . $package['current_version'] . '@' .
1117 (array_key_exists('author', $package) ? $package['author'] : 'unkownauthor')),
1118 'label' => !empty($package['label'][substr($this->lang, 0, 2)])
1119 ? $package['label'][substr($this->lang, 0, 2)]
1120 : (!empty($package['label']['en']) ? $package['label']['en'] : $package['modulename']),
1121 'description' => !empty($package['description'][substr($this->lang, 0, 2)])
1122 ? $package['description'][substr($this->lang, 0, 2)]
1123 : (!empty($package['description']['en']) ? $package['description']['en'] : ''),
1124 'datec' => (!empty($package['created_at']) && is_string($package['created_at']))
1125 ? date('Y-m-d H:i:s', strtotime($package['created_at']))
1126 : '',
1127 'tms' => (!empty($package['last_updated_at']) && is_string($package['last_updated_at']))
1128 ? date('Y-m-d H:i:s', strtotime($package['last_updated_at']))
1129 : '',
1130 'author' => array_key_exists('author', $package) ? $package['author'] : '',
1131 'author_url' => array_key_exists('author_url', $package) ? $package['author_url'] : '',
1132 'dolibarr_min' => !empty($package['dolibarrmin'])
1133 ? $package['dolibarrmin']
1134 : 'unknown',
1135 'dolibarr_max' => !empty($package['dolibarrmax'])
1136 ? $package['dolibarrmax']
1137 : 'unknown',
1138 'phpmin' => !empty($package['phpmin'])
1139 ? $package['phpmin']
1140 : 'unknown',
1141 'phpmax' => !empty($package['phpmax'])
1142 ? $package['phpmax']
1143 : 'unknown',
1144 'module_version' => !empty($package['current_version'])
1145 ? $package['current_version']
1146 : 'unknown',
1147 'cover_photo_url' => !empty($package['cover'])
1148 ? $package['cover']
1149 : '#',
1150 'category' => (!empty($package['category']) && is_string($package['category']))
1151 ? explode(',', str_replace(' ', '', (string) $package['category']))
1152 : array(),
1153 'link' => !empty($package['git'])
1154 ? $package['git']
1155 : '#',
1156 'source' => 'githubcommunity',
1157 'status' => !empty($package['status']) ? $package['status'] : '',
1158 'direct-download' => !empty($package['direct-download'])
1159 ? $package['direct-download']
1160 : '',
1161 'dolistore-download' => !empty($package['dolistore-download'])
1162 ? $package['dolistore-download']
1163 : '',
1164 ];
1165
1166 // If a price entry exists
1167 if (array_key_exists('price', $package) && $package['price'] != null) {
1168 $adaptedPackage['price_ht'] = $package['price'];
1169 }
1170
1171 $adaptedData[] = $adaptedPackage;
1172 }
1173 }
1174
1175 if ($source === 'dolistore') {
1176 foreach ($data as $package) {
1177 $urlphoto = $this->shop_url.$package['cover_photo_url'];
1178
1179 if (preg_match('/^\/?wrapper\.php\?hashp=/', $package['cover_photo_url']) && !preg_match('/attachment=/', $package['cover_photo_url'])) {
1180 $urlphoto .= '&attachment=0';
1181 }
1182
1183 $adaptedPackage = [
1184 'id' => $package['id'],
1185 'ref' => $package['ref'],
1186 'label' => $package['label'],
1187 'description' => $package['description'],
1188 'datec' => $package['datec'],
1189 'tms' => $package['tms'],
1190 'author' => array_key_exists('author', $package) ? $package['author'] : '',
1191 'author_url' => array_key_exists('author_url', $package) ? $package['author_url'] : '',
1192 'price_ttc' => $package['price_ttc'],
1193 'price_ht' => $package['price_ht'],
1194 'dolibarr_min' => $package['dolibarr_min'],
1195 'dolibarr_max' => $package['dolibarr_max'],
1196 'phpmin' => empty($package['phpmin']) ? '' : $package['phpmin'],
1197 'phpmax' => empty($package['phpmax']) ? '' : $package['phpmax'],
1198 'module_version' => $package['module_version'],
1199 'cover_photo_url' => $urlphoto,
1200 'source' => 'dolistore',
1201 'status' => empty($package['status']) ? '' : $package['status']
1202 ];
1203
1204 $adaptedData[$package['id']] = $adaptedPackage;
1205 }
1206 }
1207
1208 return $adaptedData;
1209 }
1210
1218 public function applyFilters($list, $options)
1219 {
1220 $filteredData = $list;
1221
1222 // Sort products list by datec
1223 usort(
1224 $filteredData,
1232 static function ($a, $b) {
1233 return strtotime($b['datec'] ?? '0') - strtotime($a['datec'] ?? '0');
1234 }
1235 );
1236
1237 if (!empty($options['search'])) {
1238 $filteredData = array_filter(
1239 $filteredData,
1247 static function ($package) use ($options) {
1248 return stripos($package['label'], $options['search']) !== false || stripos($package['description'], $options['search']) !== false;
1249 }
1250 );
1251 }
1252
1253 if (!empty($options['categorieid'])) {
1254 $filteredData = array_filter(
1255 $filteredData,
1263 static function ($package) use ($options) {
1264 return in_array($options['categorieid'], $package['category']);
1265 }
1266 );
1267 }
1268
1269 $total = count($filteredData);
1270
1271 // Pagination
1272 $filteredData = array_values($filteredData);
1273 $filteredData = array_slice($filteredData, ($options['page'] - 1) * $options['limit'], $options['limit']);
1274
1275 return ['total' => $total, 'data' => $filteredData];
1276 }
1277
1283 public function checkApiStatus()
1284 {
1285 // Call remote API
1286 $testRequest = $this->callApi('categories');
1287
1288 if (!isset($testRequest['response']) || !is_array($testRequest['response']) || ($testRequest['status_code'] != 200 && $testRequest['status_code'] != 201)) {
1289 $this->dolistoreApiError = $this->checkStatusCode($testRequest);
1290 return 0;
1291 } else {
1292 return 1;
1293 }
1294 }
1295
1304 public function libStatus($status, $mode = 3, $moretext = '')
1305 {
1306 global $langs;
1307
1308 $statusType = 'status4';
1309 if ($status == 0) {
1310 $statusType = 'status8';
1311 }
1312
1313 $labelStatus = [];
1314 $labelStatusShort = [];
1315
1316 $labelStatus[0] = $langs->transnoentitiesnoconv("NotConnected");
1317 $labelStatus[1] = $langs->transnoentitiesnoconv("online");
1318 $labelStatusShort[0] = $langs->transnoentitiesnoconv("NotConnected");
1319 $labelStatusShort[1] = $langs->transnoentitiesnoconv("online");
1320
1321 return dolGetStatus($labelStatus[$status], $labelStatusShort[$status], '', $statusType, $mode, '', array('badgeParams' => array('attr' => array('class' => 'classfortooltip', 'title' => $labelStatusShort[$status].$moretext))));
1322 }
1323
1324
1331 public function getModuleZIP($producttoinstall = array())
1332 {
1333 global $conf;
1334
1335 // Check if cURL is available
1336 if (!function_exists('curl_init')) {
1337 dol_syslog(__METHOD__ . ': cURL is not available', LOG_ERR);
1338 return false;
1339 }
1340
1341 // Check required fields
1342 if (empty($producttoinstall['ref'])) {
1343 dol_syslog(__METHOD__ . ': Missing producttoinstall', LOG_ERR);
1344 return false;
1345 }
1346
1347 $current_version = $producttoinstall['module_version'] ?? '';
1348 $module_name = strtolower(preg_replace('/@.*$/', '', $producttoinstall['ref'] ?? ''));
1349
1350 // Remove "-" followed by current version at the end of the string if it exists
1351 $module_name = preg_replace('/-' . preg_quote($current_version, '/') . '$/', '', $module_name);
1352
1353 if (empty($module_name) || empty($current_version) || $current_version == 'unknown') {
1354 dol_syslog(__METHOD__ . ': Missing or unknown module name/version for product', LOG_ERR);
1355 return false;
1356 }
1357
1358 // Create a temporary directory for the download
1359 $tmpdir = $conf->admin->dir_temp . '/remotestoredl';
1360 dol_mkdir($tmpdir);
1361
1362 $downloaded = false;
1363 switch ($producttoinstall['source']) {
1364 case 'dolistore':
1365 if ($producttoinstall['id'] > 0) {
1366 $source_url = 'https://www.dolistore.com/_service_download.php?t=free&p=' . $producttoinstall['id'];
1367 $downloaded = $this->_downloadFile($source_url, $tmpdir);
1368 if (!$downloaded) {
1369 dol_syslog(__METHOD__ . ': Dolistore download failed: ' . $source_url, LOG_ERR);
1370 return false;
1371 }
1372 } else {
1373 dol_syslog(__METHOD__ . ': Invalid product ID for Dolistore download: ' . $producttoinstall['id'], LOG_ERR);
1374 return false;
1375 }
1376 break;
1377 case 'githubcommunity':
1378 if ($producttoinstall['direct-download'] && $producttoinstall['direct-download'] == 'yes') {
1379 $source_url = 'https://github.com/Dolibarr/dolibarr-community-modules/raw/refs/heads/main/' . $module_name . '/module_' . $module_name . '-' . $current_version . '.zip';
1380 $downloaded = $this->_downloadFile($source_url, $tmpdir);
1381 if (!$downloaded) {
1382 dol_syslog(__METHOD__ . ': GitHub community module download failed: ' . $source_url . ', Try to find a Dolistore link', LOG_WARNING);
1383 if ($producttoinstall['id'] > 0) {
1384 $source_url = 'https://www.dolistore.com/_service_download.php?t=free&p=' . $producttoinstall['id'];
1385 $downloaded = $this->_downloadFile($source_url, $tmpdir);
1386 if (!$downloaded) {
1387 dol_syslog(__METHOD__ . ': Dolistore download failed: ' . $source_url, LOG_ERR);
1388 return false;
1389 }
1390 } else {
1391 dol_syslog(__METHOD__ . ': No direct download available for this GitHub community module', LOG_ERR);
1392 return false;
1393 }
1394 }
1395 } else {
1396 dol_syslog(__METHOD__ . ': No direct download available for this GitHub community module', LOG_ERR);
1397 return false;
1398 }
1399 break;
1400 default:
1401 dol_syslog(__METHOD__ . ': Unsupported source type: ' . $producttoinstall['source'], LOG_ERR);
1402 }
1403
1404
1405 dol_syslog(__METHOD__ . ': Module downloaded successfully to: ' . $downloaded, LOG_DEBUG);
1406 return $downloaded;
1407 }
1408
1409
1417 private function _downloadFile(string $url, string $dest_path)
1418 {
1419 // HEAD request to get real filename from Content-Disposition
1420 $filename = '';
1421 $head = getURLContent($url, 'HEAD');
1422 // Try to extract filename from Content-Disposition header
1423 if (!empty($head['header'])) {
1424 if (preg_match_all('/Content-Disposition:.*filename=["\']?([^"\';\r\n]+)/i', $head['header'], $m)) {
1425 $filename = trim(end($m[1]), " \t\"'");
1426 }
1427 }
1428
1429 // If filename is not found in headers, try to extract it from URL
1430 if (empty($filename)) {
1431 $filename = basename(parse_url($url, PHP_URL_PATH));
1432 }
1433
1434 // If filename is still empty or file name ne match the expected pattern (module_modulename-version.zip), log error and return false
1435 if (empty($filename) || !preg_match('/^module_[a-z0-9_]+-[0-9]+\.[0-9]+\.[0-9]+\.zip$/i', $filename)) {
1436 dol_syslog(__METHOD__ . ': Cannot determine filename from URL: ' . $url, LOG_ERR);
1437 return false;
1438 }
1439
1440 // Download the file
1441 $response = getURLContent($url, 'GET');
1442 if (empty($response['content']) || (isset($response['http_code']) && $response['http_code'] !== 200)) {
1443 dol_syslog(
1444 __METHOD__ . ': Download failed — HTTP ' . ($response['http_code'] ?? 'unknown') . ' — ' . $url,
1445 LOG_WARNING
1446 );
1447 return false;
1448 }
1449
1450 // Write to destination
1451 $dest_file = $dest_path . '/' . $filename;
1452 if (file_exists(dol_osencode($dest_file))) { // If file already exists, try to delete it first
1453 chmod(dol_osencode($dest_file), 0755);
1454 @unlink(dol_osencode($dest_file));
1455 }
1456 $writtenfile = file_put_contents(dol_osencode($dest_file), $response['content']);
1457 if ($writtenfile === false || $writtenfile === 0) {
1458 dol_syslog(__METHOD__ . ': Cannot write file: ' . $dest_file, LOG_ERR);
1459 @unlink(dol_osencode($dest_file));
1460 return false;
1461 }
1462
1463 dol_syslog(__METHOD__ . ': Downloaded successfully to: ' . $dest_file, LOG_DEBUG);
1464 return $dest_file;
1465 }
1466}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Class ExternalModules.
__construct($debug=false)
Constructor.
libStatus($status, $mode=3, $moretext='')
Retrieve the status icon.
checkApiStatus()
Check if an Dolistore API is up.
getProducts($options)
Generate HTML for products.
versionCompare($v1, $v2)
version compare
getPagination()
Generate pagination for navigating through pages of products.
getRemoteYamlFile($file_source_url, $cache_time)
Get YAML file from remote source and put it into the cache file.
get_previous_link($text='<<')
get previous link
buildSorter(string $key)
Sort an array by a key.
getModuleZIP($producttoinstall=array())
Download a Dolibarr module from a Git repository URL or Dolistore download URL.
getCategories($active=0)
Generate HTML for categories and their children.
applyFilters($list, $options)
Apply filters to the data.
get_next_link($text='> >')
get next link
fetchModulesFromFile($options=array())
Fetch modules from a cache YAML file.
checkStatusCode($request)
Check the status code of the request.
_downloadFile(string $url, string $dest_path)
Download a remote URL to a local file using getURLContent (native Dolibarr).
readYaml($yaml)
Read a YAML string and convert it to an array.
callApi($resource, $options=false)
Test if we can access to remote Dolistore market place.
adaptData($data, $source)
Adapter data fetched from github remote source to the expected format.
loadRemoteSources($debug=false)
loadRemoteSources
get_previous_url()
get previous url
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition date.lib.php:435
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_is_file($pathoffile)
Return if path is a file.
dol_is_dir($folder)
Test if filename is a directory.
dol_now($mode='gmt')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
dolPrintHTML($s, $allowiframe=0, $moreallowedtags=array())
Return a string (that can be on several lines) ready to be output on a HTML page.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
getDolOptimizeSmallScreen()
Return if render must be optimized for small screen.
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into JavaScript code.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1, $timeoutconnect=0, $timeoutresponse=0, $otherCurlOptions=array(), $morelogsuffix='')
Function to get a content from an URL (use proxy if proxy defined).
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:487