dolibarr 23.0.3
externalModules.class.php
1<?php
2/*
3 * Copyright (C) 2025 Mohamed DAOUD <mdaoud@dolicloud.com>
4 * Copyright (C) 2025 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 = $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 // free or pay ?
464 if (array_key_exists('price_ht', $product) && price2num($product["price_ht"]) > 0) {
465 $price = '<h3>'.price(price2num($product["price_ht"], 'MT'), 0, $langs, 1, -1, -1, 'EUR').' '.$langs->trans("HT").'</h3>';
466
467 $download_link = '<a class="paddingleft paddingright" target="_blank" title="'.$langs->trans("View").'" href="'.$this->shop_url.'/product.php?id='.((int) $product['id']).'">';
468 $download_link .= img_picto('', 'url', 'class="size2x paddingright"');
469 $download_link .= '</a>';
470 } else {
471 $download_link = '#';
472 if ($product['source'] === 'dolistore') { // 0 on dolistore may mean 0 or a complementary fee to subscribe
473 $urlview = $this->shop_url.'/product.php?id='.((int) $product["id"]);
474 $price = '<h3><a href="'.$urlview.'" target="_blank">'.$langs->trans('SeeOnDoliStore').'</a></h3>';
475 } elseif ($product['source'] === 'githubcommunity') {
476 if (array_key_exists('price_ht', $product) && empty($product['price_ht'])) {
477 if ($product['status'] == 'soon') {
478 $price = '<h3>'.$langs->trans('StillInDevelopment').'</h3>';
479 } else {
480 $price = '<h3>'.$langs->trans('Free').'</h3>';
481 }
482 } else {
483 if ($product["dolistore-download"]) {
484 $price = '<h3><a href="'.$product["dolistore-download"].'" target="_blank">'.$langs->trans('SeeOnDoliStore').'</a></h3>';
485 } else {
486 $price = '<h3>'.$langs->trans('Unknown').'</h3>';
487 }
488 }
489 } else {
490 $price = '<h3>'.$langs->trans('Unknown').'</h3>';
491 }
492
493 if ($product['source'] === 'githubcommunity') {
494 $download_link = '<a class="paddingleft paddingright" target="_blank" title="'.$langs->trans("Sources").'" href="'.$product["link"].'">';
495 $download_link .= img_picto('', 'file-code', 'class="size2x paddingright colorgrey"');
496 $download_link .= '</a>';
497
498 $urlview = $product["dolistore-download"]; // In a future, we will have the download to the zip file
499 if ($urlview) {
500 $download_link .= '<a class="paddingleft paddingright" target="_blank" title="'.$langs->trans("View").'" href="'.$urlview.'" rel="noopener noreferrer">';
501 $download_link .= img_picto('', 'url', 'class="size2x"');
502 $download_link .= '</a>';
503 }
504
505 if (!empty($product['direct-download']) && $product['direct-download'] == 'yes') {
506 $reg = array();
507 if (preg_match('/https:.*\?id=(\d+)$/', $urlview, $reg)) {
508 $urldownload = 'https://www.dolistore.com/_service_download.php?t=free&p='.$reg[1];
509 $download_link .= '<a class="paddingleft paddingright" target="_blank" title="'.$langs->trans("Download").'" href="'.$urldownload.'" rel="noopener noreferrer">';
510 $download_link .= img_picto('', 'download', 'class="size2x paddingright"');
511 //$download_link .= '<img width="32" src="'.DOL_URL_ROOT.'/admin/remotestore/img/download.png" />';
512 $download_link .= '</a>';
513 }
514 }
515 } elseif ($product['source'] === 'dolistore') {
516 $urlview = $this->shop_url.'/product.php?id='.((int) $product["id"]);
517 $urldownload = 'https://www.dolistore.com/_service_download.php?t=free&p=' . $product['id'];
518 $download_link = '<a class="paddingleft paddingright" target="_blank" title="'.$langs->trans("View").'" href="'.$urlview.'">';
519 $download_link .= img_picto('', 'url', 'class="size2x"');
520 $download_link .= '</a>';
521 $download_link .= '<a class="paddingleft paddingright" target="_blank" title="'.$langs->trans("Download").'" href="'.$urldownload.'" rel="noopener noreferrer">';
522 $download_link .= img_picto('', 'download', 'class="size2x paddingright"');
523 //$download_link .= '<img width="32" src="'.DOL_URL_ROOT.'/admin/remotestore/img/download.png" />';
524 $download_link .= '</a>';
525 }
526 }
527
528 // Set and check version
529 $version = '';
530 $compatible = '';
531 if ($product["status"] == 'soon' || $product["status"] == 'development' || $product["status"] == 'experimental') {
532 $version = '<span class="warning">'.$langs->trans("NotYetAvailable").' - '.$langs->trans("StillInDevelopment").'</span>';
533 $compatible = 'NotCompatible';
534 } elseif ($this->versionCompare($product["dolibarr_min"], $dolibarrversiontouse) <= 0) {
535 if (!empty($product["dolibarr_max"]) && $product["dolibarr_max"] != 'auto' && $product["dolibarr_max"] != 'unknown' && $this->versionCompare($product["dolibarr_max"], $dolibarrversiontouse) >= 0) {
536 //compatible
537 $version = '<span class="compatible hideonsmartphone">'.$langs->trans(
538 'CompatibleUpTo',
539 $dolibarrversiontouse,
540 $product["dolibarr_min"],
541 $product["dolibarr_max"]
542 ).'</span>';
543 $compatible = '';
544 } else {
545 // never compatible, module expired
546 $version = '<span class="warning">'.$langs->trans(
547 'NotCompatible',
548 $dolibarrversiontouse,
549 $product["dolibarr_min"],
550 $product["dolibarr_max"]
551 ).'</span>';
552 $compatible = 'NotCompatible';
553 }
554 } else {
555 if ($product["dolibarr_min"] == 'auto' || $product["dolibarr_min"] != 'unknown') {
556 // never compatible, module expired
557 $version = '<span class="warning">'.$langs->trans(
558 'NotCompatible',
559 $dolibarrversiontouse,
560 $product["dolibarr_min"],
561 $product["dolibarr_max"]
562 ).'</span>';
563 $compatible = 'NotCompatible';
564 } else {
565 //need update
566 $version = '<span class="compatibleafterupdate">'.$langs->trans(
567 'CompatibleAfterUpdate',
568 $dolibarrversiontouse,
569 $product["dolibarr_min"],
570 $product["dolibarr_max"]
571 ).'</span>';
572 $compatible = 'NotCompatible';
573 }
574 }
575
576 // Output the line
577 $html .= '<tr class="'.(getDolOptimizeSmallScreen() ? 'app' : 'app app2').' oddeven nohover '.dol_escape_htmltag($compatible).'">';
578
579 // Logo
580 $html .= '<td class="center width150"><div class="newAppParent">';
581 $html .= $newapp.$images; // No dol_escape_htmltag, it is already escape html
582 $html .= '</div></td>';
583
584 // Description
585 $html .= '<td class="margeCote minwidth500imp"><h2 class="appTitle">';
586 $html .= dolPrintHTML(dol_string_nohtmltag(ucfirst($product["label"])));
587 if (!empty($product['author']) && $product['author'] != 'unkownauthor') {
588 $html .= '<span class="small"> &nbsp; - &nbsp; '.img_picto('', 'company', 'class="pictofixedwidth"');
589 if (!empty($product['author_url'])) {
590 $html .= '<a href="'.$product['author_url'].'" target="_blank">'.$product['author'].'</a>';
591 } else {
592 $html .= $product['author'];
593 }
594 $html .= '</span>';
595 }
596 $html .= '<br><span class="small">';
597 $html .= $version; // Version Dolibarr. No dol_escape_htmltag, it is already escape html
598 $html .= '</span>';
599 $html .= '</h2>';
600
601 $html .= '<small class="appDateCreation appRef"> ';
602 if (empty($product['tms'])) {
603 $html .= img_picto($langs->trans('DateCreation'), 'calendar', 'class="pictofixedwidth"').'<span class="opacitymedium"><span class="hideonsmartphone">'.$langs->trans("DateCreation").': </span>';
604 $html .= (!empty($product['datec']) ? dol_print_date(dol_stringtotime($product['datec']), 'day') : $langs->trans("Unknown")).'</span>';
605 } else {
606 $html .= img_picto($langs->trans('DateModification'), 'calendar', 'class="pictofixedwidth"').'<span class="opacitymedium">'.dol_print_date(dol_stringtotime($product['tms']), 'day').'</span>';
607 }
608 $html .= ' &nbsp; '.$langs->trans('Ref').' '.dolPrintHTML(preg_replace('/@.*$/', '', $product["ref"]));
609 //$html .= ' - '.dol_escape_htmltag($langs->trans('Id')).': '.((int) $product["id"]);
610 $html .= '</small><br>';
611 //$html .= '<div class="appSource valignmiddle inline-block">'.$langs->trans('Source').' &nbsp; </div>';
612 $html .= '<div class="appSource valignmiddle inline-block">';
613 if ($product["source"] == 'dolistore') {
614 //$html .= img_picto('DoliStore', 'shop', 'class="pictofixedwidth"');
615 $html .= '<img border="0" title="'.dolPrintHTML($langs->trans('Source').": DoliStore").'" class="imgautosize imgmaxwidth100 valignmiddle" style="height: 14px" src="'.DOL_URL_ROOT.'/theme/dolistore_squarred.svg">';
616 } elseif ($product["source"] == 'githubcommunity') {
617 $html .= img_picto($langs->trans('Source').': GitHub community repo', 'group', 'class="pictofixedwidth valignmiddle"');
618 } else {
619 $html .= img_picto($langs->trans('Source').': '.$langs->trans('Other'), 'generic', 'class="pictofixedwidth"');
620 }
621 //$html .= $product["source"];
622 $html .= '</div> &nbsp;';
623 if (!empty($product['phpmin']) && $product['phpmin'] != 'unknown') {
624 $html .= ' <span class="badge-secondary small" style="padding: 3px; border-radius: 5px">PHP min '.$product['phpmin'].'</span>';
625 }
626 if (!empty($product['phpmax']) && $product['phpmax'] != 'unknown') {
627 $html .= ' <span class="badge-secondary small" style="padding: 3px; border-radius: 5px">PHP max '.$product['phpmax'].'</span>';
628 }
629 $html .= '<br>';
630
631 $html .= '<br>';
632 $html .= '<div class="storedesc">'.dolPrintHTML(dol_string_nohtmltag($product["description"])).'</div>';
633 $html .= '</td>';
634
636 $html .= '</tr><tr class="app2 oddeven nohover borderbottom '.dol_escape_htmltag($compatible).'">';
637 }
638
639 // Price - do not load if display none
640 $html .= '<td class="margeCote center amount'.(getDolOptimizeSmallScreen() ? ' left" colspan="2"' : '"').'>';
641 $html .= $price;
642
644 $html .= '</td>';
645 $html .= '<td class="margeCote nowraponall">';
646 }
647
648 // Links
649 $html .= $download_link;
650 $html .= '</td>';
651
652 $html .= '</tr>';
653 }
654
655 if (empty($this->products)) {
656 $colspan = (getDolOptimizeSmallScreen() ? 1 : 3);
657 $langs->load("website");
658
659 $html .= '<tr class=""><td colspan="'.$colspan.'" class="center">';
660 $html .= '<br><br>';
661 $html .= $langs->trans("noResultsWereFound").'...';
662 $html .= '<br><br>';
663 $html .= '</td></tr>';
664 }
665
666 $this->numberOfProducts = count($this->products);
667
668 return $html;
669 }
670
677 public function buildSorter(string $key): Closure
678 {
679 return
685 function (array $a, array $b) use ($key) {
686 $valA = isset($a[$key]) && is_scalar($a[$key]) ? (string) $a[$key] : '';
687 $valB = isset($b[$key]) && is_scalar($b[$key]) ? (string) $b[$key] : '';
688
689 return strnatcmp($valA, $valB);
690 };
691 }
692
700 public function versionCompare($v1, $v2)
701 {
702 // Clean v1 and v2
703 $v1 = str_replace(array('v', 'V'), '', $v1);
704 $v2 = str_replace(array('v', 'V'), '', $v2);
705
706 $v1 = explode('.', $v1);
707 $v2 = explode('.', $v2);
708 $ret = 0;
709 $level = 0;
710 $count1 = count($v1);
711 $count2 = count($v2);
712 $maxcount = max($count1, $count2);
713 while ($level < $maxcount) {
714 $operande1 = isset($v1[$level]) ? $v1[$level] : 'x';
715 $operande2 = isset($v2[$level]) ? $v2[$level] : 'x';
716 $level++;
717 if (strtoupper($operande1) == 'X' || strtoupper($operande2) == 'X' || $operande1 == '*' || $operande2 == '*') {
718 break;
719 }
720 if ($operande1 < $operande2) {
721 $ret = -$level;
722 break;
723 }
724 if ($operande1 > $operande2) {
725 $ret = $level;
726 break;
727 }
728 }
729 //print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret.'<br>'."\n";
730 return $ret;
731 }
732
733 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
740 public function get_previous_link($text = '<<')
741 {
742 // phpcs:enable
743 return '<a href="'.$this->get_previous_url().'" class="button">'.dol_escape_htmltag($text).'</a>';
744 }
745
746 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
753 public function get_next_link($text = '>>')
754 {
755 // phpcs:enable
756 return '<a href="'.$this->get_next_url().'" class="button">'.dol_escape_htmltag($text).'</a>';
757 }
758
759 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
765 public function get_previous_url()
766 {
767 // phpcs:enable
768 $param_array = array();
769 if ($this->no_page > 1) {
770 $sub = 1;
771 } else {
772 $sub = 0;
773 }
774 if (!empty($this->search)) {
775 $param_array['search_keyword'] = $this->search;
776 }
777 $param_array['no_page'] = $this->no_page - $sub;
778 if ($this->categorie != 0) {
779 $param_array['categorie'] = $this->categorie;
780 }
781 $param = http_build_query($param_array);
782 return $this->url."&".$param;
783 }
784
785 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
791 public function get_next_url()
792 {
793 // phpcs:enable
794 $param_array = array();
795 if ($this->products !== null && count($this->products) < $this->per_page) {
796 $add = 0;
797 } else {
798 $add = 1;
799 }
800 if (!empty($this->search)) {
801 $param_array['search_keyword'] = $this->search;
802 }
803 $param_array['no_page'] = $this->no_page + $add;
804 if ($this->categorie != 0) {
805 $param_array['categorie'] = $this->categorie;
806 }
807 $param = http_build_query($param_array);
808 return $this->url."&".$param;
809 }
810
816 public function getPagination()
817 {
818
819 global $langs;
820
821 $page = $this->no_page;
822 $limit = $this->per_page;
823 $totalnboflines = $this->numberTotalOfProducts ?: 0;
824 $num = $this->numberOfProducts;
825
826 $html = "";
827
828 // Show navigation bar
829 $pagelist = '';
830 if ($page > 0 || $num > $limit) {
831 if ($totalnboflines) {
832 if ($limit > 0) {
833 $nbpages = $this->numberTotalOfPages;
834 } else {
835 $nbpages = 1;
836 }
837
838 // Show previous page
839 if ($page > 1) {
840 $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>';
841 }
842
843 $pagelist .= '<li class="pagination">';
844 $pagelist .= '<label for="page_input">Page </label>';
845 if ($this->categorie != 0) {
846 $pagelist .= '<input type="hidden" name="categorie" value="' . $this->categorie . '">';
847 }
848 $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.'">';
849 $pagelist .= ' / '.$nbpages;
850 $pagelist .= '</li>';
851
852 // Show next page
853 if ($page < $nbpages) {
854 $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>';
855 }
856 }
857 }
858
859 if ($limit || $pagelist) {
860 $html .= '<div class="pagination" style="padding: 7px;">';
861 $html .= '<ul>';
862 $html .= $pagelist;
863 $html .= '</ul>';
864 $html .= '</div>';
865 }
866
867 $html .= ajax_autoselect('.page_input');
868
869 return $html;
870 }
871
878 protected function checkStatusCode($request)
879 {
880 // Define error messages
881 $error_messages = [
882 204 => 'No content',
883 400 => 'Bad Request',
884 401 => 'Unauthorized',
885 404 => 'Not Found',
886 405 => 'Method Not Allowed',
887 500 => 'Internal Server Error',
888 ];
889
890 // If status code is 200 or 201, return an empty string
891 if ($request['status_code'] === 200 || $request['status_code'] === 201) {
892 return '';
893 }
894
895 // Get the predefined error message or use a default one
896 $error_message = $error_messages[$request['status_code']] ?? 'Unexpected HTTP status: ' . $request['status_code'];
897
898 // Append error details if available
899 if (!empty($request['response']) && isset($request['response']['errors']) && is_array($request['response']['errors'])) {
900 foreach ($request['response']['errors'] as $error) {
901 $error_message .= ' - (Code ' . $error['code'] . '): ' . $error['message'];
902 }
903 }
904
905 if (!empty($request['curl_error_msg'])) {
906 $error_message .= ' - ' . $request['curl_error_msg'];
907 }
908
909 // Return the formatted error message
910 return sprintf('This call to the API failed and returned an HTTP status of %d. That means: %s.', $request['status_code'], $error_message);
911 }
912
920 public function getRemoteYamlFile($file_source_url, $cache_time)
921 {
922 $yaml = '';
923 $cache_file = $this->cache_file;
924 $cache_folder = dirname($cache_file);
925
926 // Check if cache directory exists
927 if (!dol_is_dir($cache_folder)) {
928 dol_mkdir($cache_folder, DOL_DATA_ROOT);
929 }
930
931 if (!file_exists($cache_file) || filemtime($cache_file) < (dol_now() - $cache_time)) {
932 // We get remote url
933 $addheaders = array();
934 $result = getURLContent($file_source_url, 'GET', '', 1, $addheaders); // TODO Force timeout to 5 s on both connect and response.
935 if (!empty($result) && $result['http_code'] == 200) {
936 $yaml = $result['content'];
937 $result = file_put_contents($cache_file, $yaml);
938 if ($result === false) {
939 $this->error = 'Failed to create cache file: ' . $cache_file;
940 } else {
941 dolChmod($cache_file);
942 }
943 }
944 } else {
945 $yaml = file_get_contents($cache_file);
946 }
947
948 return $yaml;
949 }
950
951
958 public function readYaml($yaml)
959 {
960 $data = [];
961 $currentPackage = null;
962 $currentSection = null;
963
964 foreach (explode("\n", trim($yaml)) as $line) {
965 $trimmedLine = trim($line);
966
967 // Ignore empty lines and comments
968 if ($trimmedLine === '' || strpos($trimmedLine, '#') === 0) {
969 continue;
970 }
971
972 // Match a new package entry (e.g., "- modulename: 'helloasso'") - Found a break in file.
973 $matches = array();
974 if (preg_match('/^\s*-\s*modulename:\s*["\']?(.*?)["\']?$/', $trimmedLine, $matches)) {
975 if ($currentPackage !== null) {
976 // Add the package to $data
977 if (!empty($currentPackage['status']) && in_array($currentPackage['status'], array('enabled', 'soon'))) {
978 $data[] = $currentPackage;
979 }
980 }
981 $currentPackage = ['modulename' => $matches[1]];
982 $currentSection = null;
983 continue;
984 }
985
986 // If the key doesn't start with fr, en, es, it, de, treat it as a section
987 if (!preg_match('/^\s*(fr|en|es|it|de):\s*["\']?(.*?)["\']?$/', $trimmedLine)) {
988 $currentSection = null;
989 }
990
991 // Match a top-level key-value pair (e.g., "author: 'Dolicloud'")
992 if (preg_match('/^(\w[\w-]*):\s*["\']?(.*?)["\']?$/', $trimmedLine, $matches)) {
993 if ($currentPackage !== null) {
994 if ($currentSection) {
995 // Store in the sub section (language into label or description for example)
996 $currentPackage[$currentSection][$matches[1]] = $matches[2] === '' ? null : $matches[2];
997 } else {
998 // Store as a normal key-value pair
999 $currentPackage[$matches[1]] = $matches[2] === '' ? null : $matches[2];
1000 }
1001 }
1002
1003 // Match a nested section (e.g., "label:")
1004 if (preg_match('/^\s*(label|description):\s*$/', $trimmedLine, $matches)) {
1005 $currentSection = $matches[1];
1006 $currentPackage[$currentSection] = []; // Initialize as an empty array for nested sections
1007 }
1008
1009 continue;
1010 }
1011 }
1012
1013 // Add the last package if available
1014 if ($currentPackage !== null) {
1015 if (!empty($currentPackage['status']) && in_array($currentPackage['status'], array('enabled', 'soon'))) {
1016 $data[] = $currentPackage;
1017 }
1018 }
1019
1020 return $data;
1021 }
1022
1030 public function adaptData($data, $source)
1031 {
1032 $adaptedData = [];
1033
1034 if (!is_array($data) || empty($data) || empty($source)) {
1035 return $adaptedData;
1036 }
1037
1038 if ($source === 'githubcommunity') {
1039 foreach ($data as $package) {
1040 if (empty($package['modulename'])) {
1041 continue;
1042 }
1043
1044 // Check if there is a known ID
1045 $reg = array();
1046 $id = 0;
1047 if (!empty($package['dolistore-download']) && preg_match('/www\.dolistore\.com\/product\.php\?id=(\d+)/', (string) $package['dolistore-download'], $reg)) {
1048 $id = $reg[1];
1049 }
1050
1051 $adaptedPackage = [
1052 'id' => $id,
1053 'ref' => str_replace(' ', '', $package['modulename'] . '-' . $package['current_version'] . '@' .
1054 (array_key_exists('author', $package) ? $package['author'] : 'unkownauthor')),
1055 'label' => !empty($package['label'][substr($this->lang, 0, 2)])
1056 ? $package['label'][substr($this->lang, 0, 2)]
1057 : (!empty($package['label']['en']) ? $package['label']['en'] : $package['modulename']),
1058 'description' => !empty($package['description'][substr($this->lang, 0, 2)])
1059 ? $package['description'][substr($this->lang, 0, 2)]
1060 : (!empty($package['description']['en']) ? $package['description']['en'] : ''),
1061 'datec' => (!empty($package['created_at']) && is_string($package['created_at']))
1062 ? date('Y-m-d H:i:s', strtotime($package['created_at']))
1063 : '',
1064 'tms' => (!empty($package['last_updated_at']) && is_string($package['last_updated_at']))
1065 ? date('Y-m-d H:i:s', strtotime($package['last_updated_at']))
1066 : '',
1067 'author' => array_key_exists('author', $package) ? $package['author'] : '',
1068 'author_url' => array_key_exists('author_url', $package) ? $package['author_url'] : '',
1069 'dolibarr_min' => !empty($package['dolibarrmin'])
1070 ? $package['dolibarrmin']
1071 : 'unknown',
1072 'dolibarr_max' => !empty($package['dolibarrmax'])
1073 ? $package['dolibarrmax']
1074 : 'unknown',
1075 'phpmin' => !empty($package['phpmin'])
1076 ? $package['phpmin']
1077 : 'unknown',
1078 'phpmax' => !empty($package['phpmax'])
1079 ? $package['phpmax']
1080 : 'unknown',
1081 'module_version' => !empty($package['current_version'])
1082 ? $package['current_version']
1083 : 'unknown',
1084 'cover_photo_url' => !empty($package['cover'])
1085 ? $package['cover']
1086 : '#',
1087 'category' => (!empty($package['category']) && is_string($package['category']))
1088 ? explode(',', str_replace(' ', '', (string) $package['category']))
1089 : array(),
1090 'link' => !empty($package['git'])
1091 ? $package['git']
1092 : '#',
1093 'source' => 'githubcommunity',
1094 'status' => !empty($package['status']) ? $package['status'] : '',
1095 'direct-download' => !empty($package['direct-download'])
1096 ? $package['direct-download']
1097 : '',
1098 'dolistore-download' => !empty($package['dolistore-download'])
1099 ? $package['dolistore-download']
1100 : '',
1101 ];
1102
1103 // If a price entry exists
1104 if (array_key_exists('price', $package) && $package['price'] != null) {
1105 $adaptedPackage['price_ht'] = $package['price'];
1106 }
1107
1108 $adaptedData[] = $adaptedPackage;
1109 }
1110 }
1111
1112 if ($source === 'dolistore') {
1113 foreach ($data as $package) {
1114 $urlphoto = $this->shop_url.$package['cover_photo_url'];
1115
1116 if (preg_match('/^\/?wrapper\.php\?hashp=/', $package['cover_photo_url']) && !preg_match('/attachment=/', $package['cover_photo_url'])) {
1117 $urlphoto .= '&attachment=0';
1118 }
1119
1120 $adaptedPackage = [
1121 'id' => $package['id'],
1122 'ref' => $package['ref'],
1123 'label' => $package['label'],
1124 'description' => $package['description'],
1125 'datec' => $package['datec'],
1126 'tms' => $package['tms'],
1127 'author' => array_key_exists('author', $package) ? $package['author'] : '',
1128 'author_url' => array_key_exists('author_url', $package) ? $package['author_url'] : '',
1129 'price_ttc' => $package['price_ttc'],
1130 'price_ht' => $package['price_ht'],
1131 'dolibarr_min' => $package['dolibarr_min'],
1132 'dolibarr_max' => $package['dolibarr_max'],
1133 'phpmin' => empty($package['phpmin']) ? '' : $package['phpmin'],
1134 'phpmax' => empty($package['phpmax']) ? '' : $package['phpmax'],
1135 'module_version' => $package['module_version'],
1136 'cover_photo_url' => $urlphoto,
1137 'source' => 'dolistore',
1138 'status' => empty($package['status']) ? '' : $package['status']
1139 ];
1140
1141 $adaptedData[$package['id']] = $adaptedPackage;
1142 }
1143 }
1144
1145 return $adaptedData;
1146 }
1147
1155 public function applyFilters($list, $options)
1156 {
1157 $filteredData = $list;
1158
1159 // Sort products list by datec
1160 usort(
1161 $filteredData,
1169 static function ($a, $b) {
1170 return strtotime($b['datec'] ?? '0') - strtotime($a['datec'] ?? '0');
1171 }
1172 );
1173
1174 if (!empty($options['search'])) {
1175 $filteredData = array_filter(
1176 $filteredData,
1184 static function ($package) use ($options) {
1185 return stripos($package['label'], $options['search']) !== false || stripos($package['description'], $options['search']) !== false;
1186 }
1187 );
1188 }
1189
1190 if (!empty($options['categorieid'])) {
1191 $filteredData = array_filter(
1192 $filteredData,
1200 static function ($package) use ($options) {
1201 return in_array($options['categorieid'], $package['category']);
1202 }
1203 );
1204 }
1205
1206 $total = count($filteredData);
1207
1208 // Pagination
1209 $filteredData = array_values($filteredData);
1210 $filteredData = array_slice($filteredData, ($options['page'] - 1) * $options['limit'], $options['limit']);
1211
1212 return ['total' => $total, 'data' => $filteredData];
1213 }
1214
1220 public function checkApiStatus()
1221 {
1222 // Call remote API
1223 $testRequest = $this->callApi('categories');
1224
1225 if (!isset($testRequest['response']) || !is_array($testRequest['response']) || ($testRequest['status_code'] != 200 && $testRequest['status_code'] != 201)) {
1226 $this->dolistoreApiError = $this->checkStatusCode($testRequest);
1227 return 0;
1228 } else {
1229 return 1;
1230 }
1231 }
1232
1241 public function libStatus($status, $mode = 3, $moretext = '')
1242 {
1243 global $langs;
1244
1245 $statusType = 'status4';
1246 if ($status == 0) {
1247 $statusType = 'status8';
1248 }
1249
1250 $labelStatus = [];
1251 $labelStatusShort = [];
1252
1253 $labelStatus[0] = $langs->transnoentitiesnoconv("NotConnected");
1254 $labelStatus[1] = $langs->transnoentitiesnoconv("online");
1255 $labelStatusShort[0] = $langs->transnoentitiesnoconv("NotConnected");
1256 $labelStatusShort[1] = $langs->transnoentitiesnoconv("online");
1257
1258 return dolGetStatus($labelStatus[$status], $labelStatusShort[$status], '', $statusType, $mode, '', array('badgeParams' => array('attr' => array('class' => 'classfortooltip', 'title' => $labelStatusShort[$status].$moretext))));
1259 }
1260}
$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.
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.
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:434
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)
Return a string (that can be on several lines) ready to be output on a HTML page.
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.
ajax_autoselect($htmlname, $addlink='', $textonlink='Link')
Make content of an input box selected when we click into input field.
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
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())
Function to get a content from an URL (use proxy if proxy defined).
if(getDolGlobalString( 'TAKEPOS_SHOW_CUSTOMER')) print $langs trans('Date')." left 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 PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:464