@@ -41,9 +41,9 @@ discard block |
||
| 41 | 41 | $xoTheme->addScript(REFERENCES_JS_URL . 'js/mootools-1.2-more.js'); |
| 42 | 42 | |
| 43 | 43 | if (isset($xoopsConfig) && file_exists(REFERENCES_PATH . 'language/' . $xoopsConfig['language'] . '/slimbox.js')) { |
| 44 | - $xoTheme->addScript(REFERENCES_URL . 'language/' . $xoopsConfig['language'] . '/slimbox.js'); |
|
| 44 | + $xoTheme->addScript(REFERENCES_URL . 'language/' . $xoopsConfig['language'] . '/slimbox.js'); |
|
| 45 | 45 | } else { |
| 46 | - $xoTheme->addScript(REFERENCES_JS_URL . 'js/slimbox.js'); |
|
| 46 | + $xoTheme->addScript(REFERENCES_JS_URL . 'js/slimbox.js'); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | $categories = $h_references_categories->getCategories(); |
@@ -63,42 +63,42 @@ discard block |
||
| 63 | 63 | $refFounded = false; |
| 64 | 64 | $mostRecentReferenceDate = 0; |
| 65 | 65 | if ($limit > 0) { |
| 66 | - $uniqueCategories = $h_references_articles->getDistinctCategoriesIds(); |
|
| 67 | - foreach ($uniqueCategories as $categoryId) { |
|
| 68 | - $items = array(); |
|
| 69 | - $items = $h_references_articles->getRecentArticles($start, $limit, references_utils::getModuleOption('sort_field'), references_utils::getModuleOption('sort_order'), true, $categoryId); |
|
| 70 | - $categoryTitle = isset($categories[$categoryId]) ? $categories[$categoryId]->getVar('category_title') : ''; |
|
| 71 | - $categoryWeight = isset($categories[$categoryId]) ? $categories[$categoryId]->getVar('category_weight') : 0; |
|
| 72 | - if (count($items) > 0) { |
|
| 73 | - foreach ($items as $item) { |
|
| 74 | - $articleData = array(); |
|
| 75 | - $articleData = $item->toArray(); |
|
| 76 | - if ($item->getVar('article_id') == $article_id) { |
|
| 77 | - $xoopsTpl->assign('defaultArticle', $index); |
|
| 78 | - $refFounded = true; |
|
| 79 | - } |
|
| 80 | - ++$index; |
|
| 81 | - $articleData['article_category_id'] = $categoryId; |
|
| 82 | - $articleData['article_category_title'] = $categoryTitle; |
|
| 83 | - $articleData['article_category_weight'] = $categoryWeight; |
|
| 84 | - $categoriesForTemplate[$categoryWeight . '-' . $categoryId]['articles'][] = $articleData; |
|
| 85 | - $categoriesForTemplate[$categoryWeight . '-' . $categoryId]['categoryTitle'] = $categoryTitle; |
|
| 86 | - $categoriesForTemplate[$categoryWeight . '-' . $categoryId]['categoryId'] = $categoryId; |
|
| 87 | - if ($item->getVar('article_timestamp') > $mostRecentReferenceDate) { |
|
| 88 | - $mostRecentReferenceDate = $item->getVar('article_timestamp'); |
|
| 89 | - $lastTitle = strip_tags($item->getVar('article_title', 'n')) . ', ' . $item->getVar('article_date'); |
|
| 90 | - $lastKeywords = strip_tags($item->getVar('article_text', 'n')); |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - if (!$refFounded) { |
|
| 96 | - $xoopsTpl->assign('defaultArticle', 0); |
|
| 97 | - } |
|
| 98 | - if (count($categoriesForTemplate) > 0) { |
|
| 99 | - ksort($categoriesForTemplate); |
|
| 100 | - } |
|
| 101 | - $xoopsTpl->assign('categories', $categoriesForTemplate); |
|
| 66 | + $uniqueCategories = $h_references_articles->getDistinctCategoriesIds(); |
|
| 67 | + foreach ($uniqueCategories as $categoryId) { |
|
| 68 | + $items = array(); |
|
| 69 | + $items = $h_references_articles->getRecentArticles($start, $limit, references_utils::getModuleOption('sort_field'), references_utils::getModuleOption('sort_order'), true, $categoryId); |
|
| 70 | + $categoryTitle = isset($categories[$categoryId]) ? $categories[$categoryId]->getVar('category_title') : ''; |
|
| 71 | + $categoryWeight = isset($categories[$categoryId]) ? $categories[$categoryId]->getVar('category_weight') : 0; |
|
| 72 | + if (count($items) > 0) { |
|
| 73 | + foreach ($items as $item) { |
|
| 74 | + $articleData = array(); |
|
| 75 | + $articleData = $item->toArray(); |
|
| 76 | + if ($item->getVar('article_id') == $article_id) { |
|
| 77 | + $xoopsTpl->assign('defaultArticle', $index); |
|
| 78 | + $refFounded = true; |
|
| 79 | + } |
|
| 80 | + ++$index; |
|
| 81 | + $articleData['article_category_id'] = $categoryId; |
|
| 82 | + $articleData['article_category_title'] = $categoryTitle; |
|
| 83 | + $articleData['article_category_weight'] = $categoryWeight; |
|
| 84 | + $categoriesForTemplate[$categoryWeight . '-' . $categoryId]['articles'][] = $articleData; |
|
| 85 | + $categoriesForTemplate[$categoryWeight . '-' . $categoryId]['categoryTitle'] = $categoryTitle; |
|
| 86 | + $categoriesForTemplate[$categoryWeight . '-' . $categoryId]['categoryId'] = $categoryId; |
|
| 87 | + if ($item->getVar('article_timestamp') > $mostRecentReferenceDate) { |
|
| 88 | + $mostRecentReferenceDate = $item->getVar('article_timestamp'); |
|
| 89 | + $lastTitle = strip_tags($item->getVar('article_title', 'n')) . ', ' . $item->getVar('article_date'); |
|
| 90 | + $lastKeywords = strip_tags($item->getVar('article_text', 'n')); |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + if (!$refFounded) { |
|
| 96 | + $xoopsTpl->assign('defaultArticle', 0); |
|
| 97 | + } |
|
| 98 | + if (count($categoriesForTemplate) > 0) { |
|
| 99 | + ksort($categoriesForTemplate); |
|
| 100 | + } |
|
| 101 | + $xoopsTpl->assign('categories', $categoriesForTemplate); |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | $xoopsTpl->assign('isAdmin', references_utils::isAdmin()); |
@@ -26,26 +26,26 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | function b_references_last_news_show($options) |
| 28 | 28 | { |
| 29 | - global $xoopsConfig; |
|
| 30 | - require XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 31 | - $block = array(); |
|
| 32 | - $start = 0; |
|
| 33 | - $limit = (int)$options[0]; |
|
| 34 | - if ($limit > 0) { |
|
| 35 | - $items = array(); |
|
| 36 | - $categories = array(); |
|
| 37 | - if (is_array($options) && count($options) > 1) { |
|
| 38 | - $categories = array_slice($options, 1); |
|
| 39 | - } |
|
| 40 | - $items = $h_references_articles->getRecentArticles($start, $limit, 'article_timestamp', 'DESC', true, $categories); |
|
| 41 | - if (count($items) > 0) { |
|
| 42 | - foreach ($items as $item) { |
|
| 43 | - $block['block_last_news'][] = $item->toArray(); |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - } |
|
| 29 | + global $xoopsConfig; |
|
| 30 | + require XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 31 | + $block = array(); |
|
| 32 | + $start = 0; |
|
| 33 | + $limit = (int)$options[0]; |
|
| 34 | + if ($limit > 0) { |
|
| 35 | + $items = array(); |
|
| 36 | + $categories = array(); |
|
| 37 | + if (is_array($options) && count($options) > 1) { |
|
| 38 | + $categories = array_slice($options, 1); |
|
| 39 | + } |
|
| 40 | + $items = $h_references_articles->getRecentArticles($start, $limit, 'article_timestamp', 'DESC', true, $categories); |
|
| 41 | + if (count($items) > 0) { |
|
| 42 | + foreach ($items as $item) { |
|
| 43 | + $block['block_last_news'][] = $item->toArray(); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - return $block; |
|
| 48 | + return $block; |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /** |
@@ -56,26 +56,26 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | function b_references_last_news_edit($options) |
| 58 | 58 | { |
| 59 | - global $xoopsConfig; |
|
| 60 | - include XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 61 | - $handlers = references_handler::getInstance(); |
|
| 62 | - $categories = $handlers->h_references_categories->getListArray(); |
|
| 63 | - $form = ''; |
|
| 64 | - $form .= "<table border='0'>"; |
|
| 65 | - $form .= '<tr><td>' . _MB_REFERENCES_ITEMS_COUNT . "</td><td><input type='text' name='options[]' id='options' value='" . $options[0] . "' /></td></tr>\n"; |
|
| 66 | - $form .= '<tr><td>' . _MB_REFERENCES_CATEGORIES . "</td><td><select name='options[]' multiple='multiple'>"; |
|
| 67 | - $size = count($options); |
|
| 68 | - foreach ($categories as $Idcategory => $categoryName) { |
|
| 69 | - $sel = ''; |
|
| 70 | - for ($i = 1; $i < $size; ++$i) { |
|
| 71 | - if ($options[$i] == $Idcategory) { |
|
| 72 | - $sel = " selected='selected'"; |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - $form .= "<option value='$Idcategory' $sel>$categoryName</option>"; |
|
| 76 | - } |
|
| 77 | - $form .= "</select></td></tr>\n"; |
|
| 78 | - $form .= '</table>'; |
|
| 59 | + global $xoopsConfig; |
|
| 60 | + include XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 61 | + $handlers = references_handler::getInstance(); |
|
| 62 | + $categories = $handlers->h_references_categories->getListArray(); |
|
| 63 | + $form = ''; |
|
| 64 | + $form .= "<table border='0'>"; |
|
| 65 | + $form .= '<tr><td>' . _MB_REFERENCES_ITEMS_COUNT . "</td><td><input type='text' name='options[]' id='options' value='" . $options[0] . "' /></td></tr>\n"; |
|
| 66 | + $form .= '<tr><td>' . _MB_REFERENCES_CATEGORIES . "</td><td><select name='options[]' multiple='multiple'>"; |
|
| 67 | + $size = count($options); |
|
| 68 | + foreach ($categories as $Idcategory => $categoryName) { |
|
| 69 | + $sel = ''; |
|
| 70 | + for ($i = 1; $i < $size; ++$i) { |
|
| 71 | + if ($options[$i] == $Idcategory) { |
|
| 72 | + $sel = " selected='selected'"; |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + $form .= "<option value='$Idcategory' $sel>$categoryName</option>"; |
|
| 76 | + } |
|
| 77 | + $form .= "</select></td></tr>\n"; |
|
| 78 | + $form .= '</table>'; |
|
| 79 | 79 | |
| 80 | - return $form; |
|
| 80 | + return $form; |
|
| 81 | 81 | } |
@@ -22,44 +22,44 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | function references_tag_block_cloud_show($options) |
| 24 | 24 | { |
| 25 | - require XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 26 | - if (!references_utils::tagModuleExists()) { |
|
| 27 | - return false; |
|
| 28 | - } |
|
| 29 | - require_once XOOPS_ROOT_PATH . '/modules/tag/blocks/block.php'; |
|
| 25 | + require XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 26 | + if (!references_utils::tagModuleExists()) { |
|
| 27 | + return false; |
|
| 28 | + } |
|
| 29 | + require_once XOOPS_ROOT_PATH . '/modules/tag/blocks/block.php'; |
|
| 30 | 30 | |
| 31 | - return tag_block_cloud_show($options, 'references'); |
|
| 31 | + return tag_block_cloud_show($options, 'references'); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | function references_tag_block_cloud_edit($options) |
| 35 | 35 | { |
| 36 | - require XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 37 | - if (!references_utils::tagModuleExists()) { |
|
| 38 | - return false; |
|
| 39 | - } |
|
| 40 | - require_once XOOPS_ROOT_PATH . '/modules/tag/blocks/block.php'; |
|
| 36 | + require XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 37 | + if (!references_utils::tagModuleExists()) { |
|
| 38 | + return false; |
|
| 39 | + } |
|
| 40 | + require_once XOOPS_ROOT_PATH . '/modules/tag/blocks/block.php'; |
|
| 41 | 41 | |
| 42 | - return tag_block_cloud_edit($options); |
|
| 42 | + return tag_block_cloud_edit($options); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | function references_tag_block_top_show($options) |
| 46 | 46 | { |
| 47 | - require XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 48 | - if (!references_utils::tagModuleExists()) { |
|
| 49 | - return false; |
|
| 50 | - } |
|
| 51 | - require_once XOOPS_ROOT_PATH . '/modules/tag/blocks/block.php'; |
|
| 47 | + require XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 48 | + if (!references_utils::tagModuleExists()) { |
|
| 49 | + return false; |
|
| 50 | + } |
|
| 51 | + require_once XOOPS_ROOT_PATH . '/modules/tag/blocks/block.php'; |
|
| 52 | 52 | |
| 53 | - return tag_block_top_show($options, 'references'); |
|
| 53 | + return tag_block_top_show($options, 'references'); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | function references_tag_block_top_edit($options) |
| 57 | 57 | { |
| 58 | - require XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 59 | - if (!references_utils::tagModuleExists()) { |
|
| 60 | - return false; |
|
| 61 | - } |
|
| 62 | - require_once XOOPS_ROOT_PATH . '/modules/tag/blocks/block.php'; |
|
| 58 | + require XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 59 | + if (!references_utils::tagModuleExists()) { |
|
| 60 | + return false; |
|
| 61 | + } |
|
| 62 | + require_once XOOPS_ROOT_PATH . '/modules/tag/blocks/block.php'; |
|
| 63 | 63 | |
| 64 | - return tag_block_top_edit($options); |
|
| 64 | + return tag_block_top_edit($options); |
|
| 65 | 65 | } |
@@ -26,28 +26,28 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | function b_references_random_news_show($options) |
| 28 | 28 | { |
| 29 | - global $xoopsConfig; |
|
| 30 | - require XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 31 | - $block = array(); |
|
| 32 | - $start = 0; |
|
| 33 | - $limit = (int)$options[0]; |
|
| 34 | - if ($limit > 0) { |
|
| 35 | - $items = array(); |
|
| 36 | - $h_references_articles->setCachingOptions(array('cacheDir' => REFERENCES_CACHE_PATH, 'caching' => false, 'lifeTime' => null, 'automaticSerialization' => true, 'fileNameProtection' => false)); |
|
| 37 | - $categories = array(); |
|
| 38 | - if (is_array($options) && count($options) > 1) { |
|
| 39 | - $categories = array_slice($options, 1); |
|
| 40 | - } |
|
| 41 | - $items = $h_references_articles->getRecentArticles($start, $limit, 'RAND(), NOW()', 'DESC', true, $categories); |
|
| 42 | - $h_references_articles->setCachingOptions(array('cacheDir' => REFERENCES_CACHE_PATH, 'caching' => true, 'lifeTime' => null, 'automaticSerialization' => true, 'fileNameProtection' => false)); |
|
| 43 | - if (count($items) > 0) { |
|
| 44 | - foreach ($items as $item) { |
|
| 45 | - $block['block_random_news'][] = $item->toArray(); |
|
| 46 | - } |
|
| 47 | - } |
|
| 48 | - } |
|
| 29 | + global $xoopsConfig; |
|
| 30 | + require XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 31 | + $block = array(); |
|
| 32 | + $start = 0; |
|
| 33 | + $limit = (int)$options[0]; |
|
| 34 | + if ($limit > 0) { |
|
| 35 | + $items = array(); |
|
| 36 | + $h_references_articles->setCachingOptions(array('cacheDir' => REFERENCES_CACHE_PATH, 'caching' => false, 'lifeTime' => null, 'automaticSerialization' => true, 'fileNameProtection' => false)); |
|
| 37 | + $categories = array(); |
|
| 38 | + if (is_array($options) && count($options) > 1) { |
|
| 39 | + $categories = array_slice($options, 1); |
|
| 40 | + } |
|
| 41 | + $items = $h_references_articles->getRecentArticles($start, $limit, 'RAND(), NOW()', 'DESC', true, $categories); |
|
| 42 | + $h_references_articles->setCachingOptions(array('cacheDir' => REFERENCES_CACHE_PATH, 'caching' => true, 'lifeTime' => null, 'automaticSerialization' => true, 'fileNameProtection' => false)); |
|
| 43 | + if (count($items) > 0) { |
|
| 44 | + foreach ($items as $item) { |
|
| 45 | + $block['block_random_news'][] = $item->toArray(); |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - return $block; |
|
| 50 | + return $block; |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
@@ -58,26 +58,26 @@ discard block |
||
| 58 | 58 | */ |
| 59 | 59 | function b_references_random_news_edit($options) |
| 60 | 60 | { |
| 61 | - global $xoopsConfig; |
|
| 62 | - include XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 63 | - $handlers = references_handler::getInstance(); |
|
| 64 | - $categories = $handlers->h_references_categories->getListArray(); |
|
| 65 | - $form = ''; |
|
| 66 | - $form .= "<table border='0'>"; |
|
| 67 | - $form .= '<tr><td>' . _MB_REFERENCES_ITEMS_COUNT . "</td><td><input type='text' name='options[]' id='options' value='" . $options[0] . "' /></td></tr>\n"; |
|
| 68 | - $form .= '<tr><td>' . _MB_REFERENCES_CATEGORIES . "</td><td><select name='options[]' multiple='multiple'>"; |
|
| 69 | - $size = count($options); |
|
| 70 | - foreach ($categories as $Idcategory => $categoryName) { |
|
| 71 | - $sel = ''; |
|
| 72 | - for ($i = 1; $i < $size; ++$i) { |
|
| 73 | - if ($options[$i] == $Idcategory) { |
|
| 74 | - $sel = " selected='selected'"; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - $form .= "<option value='$Idcategory' $sel>$categoryName</option>"; |
|
| 78 | - } |
|
| 79 | - $form .= "</select></td></tr>\n"; |
|
| 80 | - $form .= '</table>'; |
|
| 61 | + global $xoopsConfig; |
|
| 62 | + include XOOPS_ROOT_PATH . '/modules/references/include/common.php'; |
|
| 63 | + $handlers = references_handler::getInstance(); |
|
| 64 | + $categories = $handlers->h_references_categories->getListArray(); |
|
| 65 | + $form = ''; |
|
| 66 | + $form .= "<table border='0'>"; |
|
| 67 | + $form .= '<tr><td>' . _MB_REFERENCES_ITEMS_COUNT . "</td><td><input type='text' name='options[]' id='options' value='" . $options[0] . "' /></td></tr>\n"; |
|
| 68 | + $form .= '<tr><td>' . _MB_REFERENCES_CATEGORIES . "</td><td><select name='options[]' multiple='multiple'>"; |
|
| 69 | + $size = count($options); |
|
| 70 | + foreach ($categories as $Idcategory => $categoryName) { |
|
| 71 | + $sel = ''; |
|
| 72 | + for ($i = 1; $i < $size; ++$i) { |
|
| 73 | + if ($options[$i] == $Idcategory) { |
|
| 74 | + $sel = " selected='selected'"; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + $form .= "<option value='$Idcategory' $sel>$categoryName</option>"; |
|
| 78 | + } |
|
| 79 | + $form .= "</select></td></tr>\n"; |
|
| 80 | + $form .= '</table>'; |
|
| 81 | 81 | |
| 82 | - return $form; |
|
| 82 | + return $form; |
|
| 83 | 83 | } |
@@ -28,11 +28,11 @@ discard block |
||
| 28 | 28 | @$xoopsLogger->activated = false; |
| 29 | 29 | |
| 30 | 30 | if (!references_utils::getModuleOption('use_rss')) { |
| 31 | - exit(_ERRORS); |
|
| 31 | + exit(_ERRORS); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | if (function_exists('mb_http_output')) { |
| 35 | - mb_http_output('pass'); |
|
| 35 | + mb_http_output('pass'); |
|
| 36 | 36 | } |
| 37 | 37 | $charset = 'utf-8'; |
| 38 | 38 | header('Content-Type:text/xml; charset=' . $charset); |
@@ -42,52 +42,52 @@ discard block |
||
| 42 | 42 | $tpl->xoops_setCacheTime(references_utils::getModuleOption('rss_cache_time')); // Temps de cache en secondes |
| 43 | 43 | $uid = references_utils::getCurrentUserID(); |
| 44 | 44 | if (!$tpl->is_cached('db:references_rss.tpl', $uid)) { |
| 45 | - $categoryTitle = ''; |
|
| 46 | - global $xoopsConfig; |
|
| 47 | - $sitename = htmlspecialchars($xoopsConfig['sitename'], ENT_QUOTES); |
|
| 48 | - $email = $xoopsConfig['adminmail']; |
|
| 49 | - $slogan = htmlspecialchars($xoopsConfig['slogan'], ENT_QUOTES); |
|
| 45 | + $categoryTitle = ''; |
|
| 46 | + global $xoopsConfig; |
|
| 47 | + $sitename = htmlspecialchars($xoopsConfig['sitename'], ENT_QUOTES); |
|
| 48 | + $email = $xoopsConfig['adminmail']; |
|
| 49 | + $slogan = htmlspecialchars($xoopsConfig['slogan'], ENT_QUOTES); |
|
| 50 | 50 | |
| 51 | - $tpl->assign('charset', $charset); |
|
| 52 | - $tpl->assign('channel_title', xoops_utf8_encode($sitename)); |
|
| 53 | - $tpl->assign('channel_link', XOOPS_URL . '/'); |
|
| 54 | - $tpl->assign('channel_desc', xoops_utf8_encode($slogan)); |
|
| 55 | - $tpl->assign('channel_lastbuild', formatTimestamp(time(), 'rss')); |
|
| 56 | - $tpl->assign('channel_webmaster', xoops_utf8_encode($email)); |
|
| 57 | - $tpl->assign('channel_editor', xoops_utf8_encode($email)); |
|
| 58 | - $tpl->assign('channel_category', xoops_utf8_encode($categoryTitle)); |
|
| 59 | - $tpl->assign('channel_generator', xoops_utf8_encode(references_utils::getModuleName())); |
|
| 60 | - $tpl->assign('channel_language', _LANGCODE); |
|
| 61 | - $tpl->assign('image_url', XOOPS_URL . '/assets/images/logo.gif'); |
|
| 62 | - $dimention = getimagesize(XOOPS_ROOT_PATH . '/assets/images/logo.gif'); |
|
| 63 | - if (empty($dimention[0])) { |
|
| 64 | - $width = 88; |
|
| 65 | - } else { |
|
| 66 | - $width = ($dimention[0] > 144) ? 144 : $dimention[0]; |
|
| 67 | - } |
|
| 68 | - if (empty($dimention[1])) { |
|
| 69 | - $height = 31; |
|
| 70 | - } else { |
|
| 71 | - $height = ($dimention[1] > 400) ? 400 : $dimention[1]; |
|
| 72 | - } |
|
| 73 | - $tpl->assign('image_width', $width); |
|
| 74 | - $tpl->assign('image_height', $height); |
|
| 75 | - $start = 0; |
|
| 76 | - $limit = references_utils::getModuleOption('nb_perpage'); |
|
| 77 | - $items = array(); |
|
| 51 | + $tpl->assign('charset', $charset); |
|
| 52 | + $tpl->assign('channel_title', xoops_utf8_encode($sitename)); |
|
| 53 | + $tpl->assign('channel_link', XOOPS_URL . '/'); |
|
| 54 | + $tpl->assign('channel_desc', xoops_utf8_encode($slogan)); |
|
| 55 | + $tpl->assign('channel_lastbuild', formatTimestamp(time(), 'rss')); |
|
| 56 | + $tpl->assign('channel_webmaster', xoops_utf8_encode($email)); |
|
| 57 | + $tpl->assign('channel_editor', xoops_utf8_encode($email)); |
|
| 58 | + $tpl->assign('channel_category', xoops_utf8_encode($categoryTitle)); |
|
| 59 | + $tpl->assign('channel_generator', xoops_utf8_encode(references_utils::getModuleName())); |
|
| 60 | + $tpl->assign('channel_language', _LANGCODE); |
|
| 61 | + $tpl->assign('image_url', XOOPS_URL . '/assets/images/logo.gif'); |
|
| 62 | + $dimention = getimagesize(XOOPS_ROOT_PATH . '/assets/images/logo.gif'); |
|
| 63 | + if (empty($dimention[0])) { |
|
| 64 | + $width = 88; |
|
| 65 | + } else { |
|
| 66 | + $width = ($dimention[0] > 144) ? 144 : $dimention[0]; |
|
| 67 | + } |
|
| 68 | + if (empty($dimention[1])) { |
|
| 69 | + $height = 31; |
|
| 70 | + } else { |
|
| 71 | + $height = ($dimention[1] > 400) ? 400 : $dimention[1]; |
|
| 72 | + } |
|
| 73 | + $tpl->assign('image_width', $width); |
|
| 74 | + $tpl->assign('image_height', $height); |
|
| 75 | + $start = 0; |
|
| 76 | + $limit = references_utils::getModuleOption('nb_perpage'); |
|
| 77 | + $items = array(); |
|
| 78 | 78 | |
| 79 | - $items = $h_references_articles->getRecentArticles($start, $limit); |
|
| 80 | - foreach ($items as $item) { |
|
| 81 | - $titre = htmlspecialchars($item->getVar('article_title', 'n'), ENT_QUOTES); |
|
| 82 | - $description = htmlspecialchars($item->getVar('article_text'), ENT_QUOTES); |
|
| 83 | - $link = REFERENCES_URL; |
|
| 84 | - $tpl->append('items', array( |
|
| 85 | - 'title' => xoops_utf8_encode($titre), |
|
| 86 | - 'link' => $link, |
|
| 87 | - 'guid' => $link, |
|
| 88 | - 'pubdate' => formatTimestamp($item->getVar('article_timestamp'), 'rss'), |
|
| 89 | - 'description' => xoops_utf8_encode($description) |
|
| 90 | - )); |
|
| 91 | - } |
|
| 79 | + $items = $h_references_articles->getRecentArticles($start, $limit); |
|
| 80 | + foreach ($items as $item) { |
|
| 81 | + $titre = htmlspecialchars($item->getVar('article_title', 'n'), ENT_QUOTES); |
|
| 82 | + $description = htmlspecialchars($item->getVar('article_text'), ENT_QUOTES); |
|
| 83 | + $link = REFERENCES_URL; |
|
| 84 | + $tpl->append('items', array( |
|
| 85 | + 'title' => xoops_utf8_encode($titre), |
|
| 86 | + 'link' => $link, |
|
| 87 | + 'guid' => $link, |
|
| 88 | + 'pubdate' => formatTimestamp($item->getVar('article_timestamp'), 'rss'), |
|
| 89 | + 'description' => xoops_utf8_encode($description) |
|
| 90 | + )); |
|
| 91 | + } |
|
| 92 | 92 | } |
| 93 | 93 | $tpl->display('db:references_rss.tpl', $uid); |
@@ -35,930 +35,930 @@ |
||
| 35 | 35 | */ |
| 36 | 36 | class references_listFilter |
| 37 | 37 | { |
| 38 | - const FILTER_DATA_TEXT = 1; |
|
| 39 | - const FILTER_DATA_NUMERIC = 2; |
|
| 40 | - |
|
| 41 | - const FILTER_FIELD_TEXT = 1; |
|
| 42 | - const FILTER_FIELD_SELECT = 2; |
|
| 43 | - const FILTER_FIELD_SELECT_YES_NO = 3; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Préfixe des variables de sélection |
|
| 47 | - */ |
|
| 48 | - const PREFIX = 'filter_'; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Nom du module (utilisé pour la session) |
|
| 52 | - */ |
|
| 53 | - const MODULE_NAME = 'references'; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Contient toutes les variables participant au filtre (avec leur type et description) |
|
| 57 | - * |
|
| 58 | - * @var array |
|
| 59 | - */ |
|
| 60 | - private $vars = array(); |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Handler des données |
|
| 64 | - * |
|
| 65 | - * @var reference |
|
| 66 | - */ |
|
| 67 | - private $handler = null; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Nom de la zone d'opération (par exemple op=products) |
|
| 71 | - * |
|
| 72 | - * @var string |
|
| 73 | - */ |
|
| 74 | - private $op = ''; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * action de l'opération en cour (par exemple op=products) |
|
| 78 | - * |
|
| 79 | - * @var string |
|
| 80 | - */ |
|
| 81 | - private $operationName = ''; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Nombre maximum d'éléments pas page |
|
| 85 | - * |
|
| 86 | - * @var intger |
|
| 87 | - */ |
|
| 88 | - private $limit = 10; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Nom de la variable start |
|
| 92 | - * |
|
| 93 | - * @var string |
|
| 94 | - */ |
|
| 95 | - private $startName = ''; |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Critère {@link Criteria} servant à choisir les données |
|
| 99 | - * |
|
| 100 | - * @var object |
|
| 101 | - */ |
|
| 102 | - private $criteria = null; |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Indique s'il y a un nouveau critère |
|
| 106 | - * |
|
| 107 | - * @var boolean |
|
| 108 | - */ |
|
| 109 | - private $newFilter = false; |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Indique si la méthode filter() a été appelée |
|
| 113 | - * |
|
| 114 | - * @var boolean |
|
| 115 | - */ |
|
| 116 | - private $isInitialized = false; |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Zone de tri pour retourner les données (dans getObjects) |
|
| 120 | - * |
|
| 121 | - * @var string |
|
| 122 | - */ |
|
| 123 | - private $sortField = ''; |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Sens de tri |
|
| 127 | - * |
|
| 128 | - * @var string |
|
| 129 | - */ |
|
| 130 | - private $sortOrder = ''; |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Liste de critères par défaut |
|
| 134 | - * |
|
| 135 | - * @var array |
|
| 136 | - */ |
|
| 137 | - private $defaultCriterias = array(); |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * L'url complète du script qui appelle |
|
| 141 | - * |
|
| 142 | - * @var string |
|
| 143 | - */ |
|
| 144 | - private $baseUrl = ''; |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Indique si on conserve en session la position de départ |
|
| 148 | - * |
|
| 149 | - * @var boolean |
|
| 150 | - */ |
|
| 151 | - private $keepStart = true; |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Indique si l'autocomplétion est activée |
|
| 155 | - * |
|
| 156 | - * @var boolean |
|
| 157 | - */ |
|
| 158 | - private $hasAutoComplete = false; |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Url du dossier js dans le module |
|
| 162 | - * |
|
| 163 | - * @var string |
|
| 164 | - */ |
|
| 165 | - private $jsFolderUrl = ''; |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Paramètres additionnels à ajouter aux paramètres du pager |
|
| 169 | - * @var array |
|
| 170 | - */ |
|
| 171 | - private $additionnalPagerParameters = array(); |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Paramètres additionnels à ajouter au bouton permettant de supprimer le filtre en cours |
|
| 175 | - * @var array |
|
| 176 | - */ |
|
| 177 | - private $additionnalClearButtonParameters = array(); |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Tableau des champs triables [clé] = nom du champ dans la base, valeur = libellé |
|
| 181 | - * @var array |
|
| 182 | - */ |
|
| 183 | - private $sortFields = array(); |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Initialise les paramètres avec des valeurs par défaut |
|
| 187 | - */ |
|
| 188 | - private function setDefaultValues() |
|
| 189 | - { |
|
| 190 | - $this->handler = null; |
|
| 191 | - $this->op = ''; |
|
| 192 | - $this->limit = 10; |
|
| 193 | - $this->startName = 'start'; |
|
| 194 | - $this->operationName = 'op'; |
|
| 195 | - $this->sortField = ''; |
|
| 196 | - $this->sortOrder = 'asc'; |
|
| 197 | - $this->baseUrl = ''; |
|
| 198 | - $this->keepStart = true; |
|
| 199 | - $this->hasAutoComplete = false; |
|
| 200 | - $this->jsFolderUrl = ''; |
|
| 201 | - $this->additionnalPagerParameters = array(); |
|
| 202 | - $this->additionnalClearButtonParameters = array(); |
|
| 203 | - $this->sortFields = array(); // Les champs qui peuvent être utilisés pour trier |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Initialisation des données, handler et opération courante dans l'appelant |
|
| 208 | - * |
|
| 209 | - * @param mixed $handler Soit une référence au handler de données soit un tableau qui contient toutes les options (auxquel cas les autres paramètres sont inutiles) |
|
| 210 | - * @param string $operationName |
|
| 211 | - * @param string $operation Opération courante dans l'appelant |
|
| 212 | - * @param string $startName Nom du paramètre start |
|
| 213 | - * @param integer $limit Nombre maximum d'éléments par page |
|
| 214 | - * @param string $baseUrl L'url complète du script appelant |
|
| 215 | - * @param string $sortField Zone de tri |
|
| 216 | - * @param string $sortOrder Sens de tri |
|
| 217 | - * @param boolean $keepStart Indique si on conserve la position de départ |
|
| 218 | - * @param string $jsFolderUrl |
|
| 219 | - * @package string $jsFolderUrl Url du répertoire qui contient les scripts Javascript |
|
| 220 | - */ |
|
| 221 | - public function __construct($handler, $operationName = 'op', $operation = '', $startName = 'start', $limit = 10, $baseUrl = '', $sortField = '', $sortOrder = 'asc', $keepStart = true, $jsFolderUrl = '') |
|
| 222 | - { |
|
| 223 | - $this->setDefaultValues(); |
|
| 224 | - if (!is_array($handler)) { |
|
| 225 | - $this->handler = $handler; |
|
| 226 | - $this->op = $operation; |
|
| 227 | - $this->limit = $limit; |
|
| 228 | - $this->startName = $startName; |
|
| 229 | - $this->operationName = $operationName; |
|
| 230 | - $this->sortField = $sortField; |
|
| 231 | - $this->sortOrder = $sortOrder; |
|
| 232 | - $this->baseUrl = $baseUrl; |
|
| 233 | - $this->keepStart = $keepStart; |
|
| 234 | - $this->jsFolderUrl = $jsFolderUrl; |
|
| 235 | - } else { |
|
| 236 | - foreach ($handler as $key => $value) { |
|
| 237 | - $this->$key = $value; |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Donne à la classe le nom des champs sur lesquels on peut faire le tri |
|
| 244 | - * |
|
| 245 | - * @param array $fields [clé] = nom du champ dans la base, valeur = libellé |
|
| 246 | - * @return object |
|
| 247 | - */ |
|
| 248 | - public function setSortFields($fields) |
|
| 249 | - { |
|
| 250 | - $this->sortFields = $fields; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Retourne les noms à utiliser pour les champs de tri (sélecteur de champ et ordre de tri) |
|
| 255 | - * |
|
| 256 | - * @return array [0] = Nom du sélecteur de champs, [1] = Nom du sélecteur pour le sens du tri |
|
| 257 | - */ |
|
| 258 | - private function getSortPlaceHolderNames() |
|
| 259 | - { |
|
| 260 | - return array(self::PREFIX . 'sortFields', self::PREFIX . 'sortOrder'); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Retourne 2 sélecteurs html pour choisir la zone de tri et le sens du tri |
|
| 265 | - * |
|
| 266 | - * @return string |
|
| 267 | - */ |
|
| 268 | - public function getSortPlaceHolderHtmlCode() |
|
| 269 | - { |
|
| 270 | - $sortNames = $this->getSortPlaceHolderNames(); |
|
| 271 | - $sortFieldsHtml = references_utils::htmlSelect($sortNames[0], $this->sortFields, $this->sortField, false); |
|
| 272 | - $sortOrderHtml = references_utils::htmlSelect($sortNames[1], array('asc' => _MD_REFERENCES_ASC, 'desc' => _MD_REFERENCES_DESC), $this->sortOrder, false); |
|
| 273 | - |
|
| 274 | - return _MD_REFERENCES_SORT_BY . ' ' . $sortFieldsHtml . ' ' . $sortOrderHtml; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Permet de valoriser une option directement tout en chainant |
|
| 279 | - * |
|
| 280 | - * @param string $optionName |
|
| 281 | - * @param mixed $optionValue |
|
| 282 | - * @return object |
|
| 283 | - */ |
|
| 284 | - public function setOption($optionName, $optionValue) |
|
| 285 | - { |
|
| 286 | - $this->$optionName = $optionValue; |
|
| 287 | - |
|
| 288 | - return $this; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Ajoute un nouveau critère par défaut à la liste des critères par défaut |
|
| 293 | - * |
|
| 294 | - * @param Criteria $criteria |
|
| 295 | - */ |
|
| 296 | - public function addDefaultCriteria(Criteria $criteria) |
|
| 297 | - { |
|
| 298 | - $this->defaultCriterias[] = $criteria; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Retourne une valeur d'un tableau ou null si l'index n'existe pas |
|
| 303 | - * |
|
| 304 | - * @param array $array Le tableau à traiter |
|
| 305 | - * @param string $index L'index recherché |
|
| 306 | - * @param mixed $defaultValue La valeur par défaut |
|
| 307 | - * @return mixed |
|
| 308 | - */ |
|
| 309 | - private function getArrayValue($array, $index, $defaultValue = false) |
|
| 310 | - { |
|
| 311 | - if ($index === 'autoComplete' && isset($array[$index]) && isset($array[$index]) == true) { |
|
| 312 | - $this->hasAutoComplete = true; // On en profite pour vérifier si un champ utilise l'autocomplétion |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - return isset($array[$index]) ? $array[$index] : $defaultValue; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Permet de faire l'autocomplétion d'un champ |
|
| 320 | - * |
|
| 321 | - * @param string $query |
|
| 322 | - * @param $limit |
|
| 323 | - * @param string $fieldName |
|
| 324 | - * @return string |
|
| 325 | - */ |
|
| 326 | - public function autoComplete($query, $limit, $fieldName) |
|
| 327 | - { |
|
| 328 | - $return = ''; |
|
| 329 | - if (!$this->hasAutoComplete) { // Si aucun champ n'est en autocomplétion, c'est pas la peine d'aller plus loin |
|
| 330 | - |
|
| 331 | - return $return; |
|
| 332 | - } |
|
| 333 | - if (isset($this->vars[$fieldName])) { // On vérifie que le champ demandé est bien en autocomplétion |
|
| 334 | - if ($this->vars[$fieldName]['autoComplete'] == true) { |
|
| 335 | - if ($this->vars[$fieldName]['dataType'] == self::FILTER_DATA_TEXT) { |
|
| 336 | - $criteria = new Criteria($fieldName, $query . '%', 'LIKE'); |
|
| 337 | - } |
|
| 338 | - $criteria->setLimit((int)$limit); |
|
| 339 | - $ret = $this->handler->getObjects($criteria); |
|
| 340 | - |
|
| 341 | - if (count($ret) > 0) { |
|
| 342 | - foreach ($ret as $object) { |
|
| 343 | - $return .= $object->getVar($fieldName, 'n') . "\n"; |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - } |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - return $return; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Retourne le code Javascript à utiliser pour initialiser l'auto complétion (et donc à coller dans le code html) |
|
| 354 | - * |
|
| 355 | - * @param boolean $jqueryAlreadyLoaded Indique si jQuery est déjà chargé par l'appelant, auquel cas rien ne sert de le recharger |
|
| 356 | - * @return string |
|
| 357 | - */ |
|
| 358 | - public function getJavascriptInitCode($jqueryAlreadyLoaded = false) |
|
| 359 | - { |
|
| 360 | - $return = ''; |
|
| 361 | - if (!$this->hasAutoComplete) { |
|
| 362 | - return $return; |
|
| 363 | - } |
|
| 364 | - $return = ''; |
|
| 365 | - $return .= "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" title=\"Style sheet\" href=\"" . $this->jsFolderUrl . "autocomplete/jquery.autocomplete.css\" />\n"; |
|
| 366 | - if (!$jqueryAlreadyLoaded) { |
|
| 367 | - $return .= "<script type=\"text/javascript\" src=\"" . $this->jsFolderUrl . "jquery/jquery.js\"></script>\n"; |
|
| 368 | - } |
|
| 369 | - $return .= "<script type=\"text/javascript\" src=\"" . $this->jsFolderUrl . "noconflict.js\"></script>\n"; |
|
| 370 | - $return .= "<script type=\"text/javascript\" src=\"" . $this->jsFolderUrl . "autocomplete/jquery.autocomplete.min.js\"></script>\n"; |
|
| 371 | - $return .= "<script type=\"text/javascript\">\n"; |
|
| 372 | - $return .= "jQuery(function($) {\n"; |
|
| 373 | - $return .= "var url='" . $this->baseUrl . "';\n"; // TODO: Supprimer "var" car cela limite sa portée ! |
|
| 374 | - $return .= "var handlerName='" . $this->handler->className . "';\n"; |
|
| 375 | - |
|
| 376 | - foreach ($this->vars as $fieldName => $parameters) { |
|
| 377 | - if ($parameters['autoComplete'] == true) { |
|
| 378 | - $field = self::PREFIX . $fieldName; |
|
| 379 | - $return .= "$('#" . $field . "').autocomplete(url, {\n"; |
|
| 380 | - $return .= "extraParams: {\n"; |
|
| 381 | - $return .= " field: '" . $fieldName . "',\n"; |
|
| 382 | - $return .= " op: 'autocomplete',\n"; |
|
| 383 | - $return .= " handler: handlerName\n"; |
|
| 384 | - $return .= "}\n"; |
|
| 385 | - $return .= "});\n"; |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - $return .= "});\n"; |
|
| 389 | - $return .= "</script>\n"; |
|
| 390 | - |
|
| 391 | - return $return; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * Initialisation des données du filtre |
|
| 396 | - * Permet d'indiquer quelles sont les zones sur lesquelles on effectue des filtres ainsi que leur type |
|
| 397 | - * |
|
| 398 | - * @param string $fieldName Le nom du champs dans la table |
|
| 399 | - * @param array $parameters Les paramètres de la zone sous la forme d'un tableau : |
|
| 400 | - * [dataType] Entier représentant le type de donnée (numérique ou chaine) |
|
| 401 | - * [fieldType] Le type de zone de saisie (zone de texte, liste déroulante, liste déroulante Oui/Non) |
|
| 402 | - * [values] La ou les valeurs de la zone de saisie (utilisé dans le cas d'un select) |
|
| 403 | - * [size] Largeur d'affichage pour les textbox |
|
| 404 | - * [maxLength] Largeur maximale pour les textbox |
|
| 405 | - * [withNull] Dans le cas des listes déroulantes, indique s'il faut une valeur nulle |
|
| 406 | - * [minusOne] Indique s'il faut retrancher 1 à la valeur saisie récupérée (cas classique des listes Oui/Non avec une valeur nulle) |
|
| 407 | - * [style] Dans le cas des liste déroulante, le style à appliquer à la liste |
|
| 408 | - * [data] A ne pas renseigner, contient la valeur saisie par l'utilisateur |
|
| 409 | - * [operator] Opérateur de comparaison pour le Criteria |
|
| 410 | - * [autoComplete] Indique si on active l'auto complétion sur le champs |
|
| 411 | - * @return object L'objet courant pour pouvoir chainer |
|
| 412 | - */ |
|
| 413 | - public function initFilter($fieldName, $parameters) |
|
| 414 | - { |
|
| 415 | - // Tableau des valeurs attendues avec leur valeur par défaut |
|
| 416 | - $indexNames = array('dataType' => self::FILTER_DATA_TEXT, 'fieldType' => self::FILTER_FIELD_TEXT, 'values' => null, 'withNull' => false, 'size' => 5, 'maxLength' => 255, 'minusOne' => false, 'data' => null, 'style' => '', 'operator' => '=', 'autoComplete' => false); |
|
| 417 | - $data = array(); |
|
| 418 | - foreach ($indexNames as $indexName => $defaultValue) { |
|
| 419 | - $data[$indexName] = $this->getArrayValue($parameters, $indexName, $defaultValue); |
|
| 420 | - } |
|
| 421 | - $this->vars[$fieldName] = $data; |
|
| 422 | - |
|
| 423 | - return $this; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Retourne le nom du tableau à utiliser pour la session |
|
| 428 | - * @note : Le nom de la session est composé de : nom du module_nom du handler, par exemple references_references_articles |
|
| 429 | - * |
|
| 430 | - * @return string |
|
| 431 | - */ |
|
| 432 | - private function getSessionName() |
|
| 433 | - { |
|
| 434 | - return self::MODULE_NAME . '_' . $this->handler->table; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Retourne le nom de la clé à utiliser pour la conservation du start en session |
|
| 439 | - * |
|
| 440 | - * @return string |
|
| 441 | - */ |
|
| 442 | - private function getStartSessionName() |
|
| 443 | - { |
|
| 444 | - return $this->getSessionName() . '_start'; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * Réinitialisation des données avant traitement |
|
| 449 | - * |
|
| 450 | - * @return void |
|
| 451 | - */ |
|
| 452 | - private function reinitializeFieldsValue() |
|
| 453 | - { |
|
| 454 | - foreach ($this->vars as $fieldName => $fieldProperties) { |
|
| 455 | - if ($fieldProperties['dataType'] == self::FILTER_DATA_NUMERIC) { // Zone numérique |
|
| 456 | - $fieldProperties['data'] = 0; |
|
| 457 | - } else { |
|
| 458 | - $fieldProperties['data'] = ''; |
|
| 459 | - } |
|
| 460 | - $this->vars[$fieldName] = $fieldProperties; |
|
| 461 | - } |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Ajoute les critères par défaut au critère général |
|
| 466 | - * |
|
| 467 | - * @return void |
|
| 468 | - */ |
|
| 469 | - private function addDefaultCriterias() |
|
| 470 | - { |
|
| 471 | - if (is_array($this->defaultCriterias) && count($this->defaultCriterias) > 0) { |
|
| 472 | - foreach ($this->defaultCriterias as $criteria) { |
|
| 473 | - $this->criteria->add($criteria); |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - return $this; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - /** |
|
| 481 | - * Indique s'il y a des champs de tri |
|
| 482 | - * |
|
| 483 | - * @return boolean |
|
| 484 | - */ |
|
| 485 | - private function areThereSortFields() |
|
| 486 | - { |
|
| 487 | - $return = false; |
|
| 488 | - if (is_array($this->sortFields) && count($this->sortFields) > 0) { |
|
| 489 | - $return = true; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - return $return; |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - /** |
|
| 496 | - * Indique si le nom du champ passé en paramètre fait partie de la liste des champs "triables" |
|
| 497 | - * |
|
| 498 | - * @param string $fieldName |
|
| 499 | - * @return boolean |
|
| 500 | - */ |
|
| 501 | - private function isInSortFieldsList($fieldName) |
|
| 502 | - { |
|
| 503 | - return array_key_exists($fieldName, $this->sortFields); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - /** |
|
| 507 | - * Indique si le sens de tri passé en paramètre fait partie de la liste autorisée |
|
| 508 | - * |
|
| 509 | - * @param string $order |
|
| 510 | - * @return boolean |
|
| 511 | - */ |
|
| 512 | - private function isInSortOrderList($order) |
|
| 513 | - { |
|
| 514 | - return in_array($order, array('asc', 'desc')); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - /** |
|
| 518 | - * Récupère, depuis la requête d'entrée, la zone de tri à utiliser et le sens du tri et place l'information en cookie |
|
| 519 | - * pour que lorsque l'utilisateur se reconnecte, il retrouve ses informations de tri |
|
| 520 | - * |
|
| 521 | - * @return void |
|
| 522 | - */ |
|
| 523 | - private function setSortFieldsFromRequest() |
|
| 524 | - { |
|
| 525 | - if (!$this->areThereSortFields()) { // S'il n'y a pas de champs triables, on laisse tomber |
|
| 526 | - |
|
| 527 | - return; |
|
| 528 | - } |
|
| 529 | - $sortField = $sortOrder = ''; |
|
| 530 | - $cookieName = $this->getSessionName(); |
|
| 531 | - $orderFieldsNames = $this->getSortPlaceHolderNames(); |
|
| 532 | - if (isset($_REQUEST[$orderFieldsNames[0]]) && isset($_REQUEST[$orderFieldsNames[1]])) { |
|
| 533 | - $sortField = $_REQUEST[$orderFieldsNames[0]]; |
|
| 534 | - $sortOrder = $_REQUEST[$orderFieldsNames[1]]; |
|
| 535 | - } else { |
|
| 536 | - if (isset($_SESSION[$cookieName . '_sortField'])) { |
|
| 537 | - $sortField = $_SESSION[$cookieName . '_sortField']; |
|
| 538 | - } |
|
| 539 | - if (isset($_SESSION[$cookieName . '_sortOrder'])) { |
|
| 540 | - $sortOrder = $_SESSION[$cookieName . '_sortOrder']; |
|
| 541 | - } |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - if ($this->isInSortFieldsList($sortField) && $this->isInSortOrderList($sortOrder)) { |
|
| 545 | - $this->sortField = $sortField; |
|
| 546 | - $this->sortOrder = $sortOrder; |
|
| 547 | - } |
|
| 548 | - if (trim($sortField) != '' && trim($sortOrder) != '') { |
|
| 549 | - $_SESSION[$cookieName . '_sortField'] = $sortField; |
|
| 550 | - $_SESSION[$cookieName . '_sortOrder'] = $sortOrder; |
|
| 551 | - } |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * Réinitialisation des données avant traitement |
|
| 556 | - * |
|
| 557 | - * @return void |
|
| 558 | - */ |
|
| 559 | - private function setupFilter() |
|
| 560 | - { |
|
| 561 | - $this->reinitializeFieldsValue(); |
|
| 562 | - $this->newFilter = false; |
|
| 563 | - $this->isInitialized = true; |
|
| 564 | - $this->criteria = new CriteriaCompo(); |
|
| 565 | - $this->criteria->add(new Criteria($this->handler->keyName, 0, '<>')); |
|
| 566 | - $this->addDefaultCriterias(); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * RAZ des données du filtre si cela a été demandé dans la requête |
|
| 571 | - * |
|
| 572 | - * @return void |
|
| 573 | - */ |
|
| 574 | - private function isSetCleanFilter() |
|
| 575 | - { |
|
| 576 | - if (isset($_REQUEST['cleanFilter'])) { |
|
| 577 | - $this->setStartInSession(0); |
|
| 578 | - unset($_SESSION[$this->getSessionName()]); |
|
| 579 | - } |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - /** |
|
| 583 | - * Retourne le critère de filtrage courant |
|
| 584 | - * |
|
| 585 | - * @return object |
|
| 586 | - */ |
|
| 587 | - public function getCriteria() |
|
| 588 | - { |
|
| 589 | - return $this->criteria; |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - /** |
|
| 593 | - * Méthode à appeler juste après le constructeur pour qu'elle récupère les données saisies |
|
| 594 | - * |
|
| 595 | - * @return object L'objet courant pour pouvoir chainer |
|
| 596 | - */ |
|
| 597 | - public function filter() |
|
| 598 | - { |
|
| 599 | - $this->setupFilter(); // Réinitialisations |
|
| 600 | - $ts = MyTextSanitizer::getInstance(); |
|
| 601 | - $this->setSortFieldsFromRequest(); // On récupère la zone de tri éventuellement passée dans la requête |
|
| 602 | - $this->isSetCleanFilter(); |
|
| 603 | - |
|
| 604 | - foreach ($this->vars as $fieldName => $fieldProperties) { |
|
| 605 | - // On commence par récupérer toutes les valeurs |
|
| 606 | - $formFieldName = self::PREFIX . $fieldName; // "filter_website_id" par exemple |
|
| 607 | - $fieldProperties['data'] = null; // Valeur par défaut |
|
| 608 | - if (isset($_REQUEST[$formFieldName])) { |
|
| 609 | - if ($fieldProperties['dataType'] == self::FILTER_DATA_NUMERIC) { // Zone numérique |
|
| 610 | - if ((int)$_REQUEST[$formFieldName] != 0) { |
|
| 611 | - $fieldProperties['data'] = (int)$_REQUEST[$formFieldName]; |
|
| 612 | - if (!$fieldProperties['minusOne']) { |
|
| 613 | - $this->criteria->add(new Criteria($fieldName, $fieldProperties['data'], $fieldProperties['operator'])); |
|
| 614 | - } else { |
|
| 615 | - $this->criteria->add(new Criteria($fieldName, $fieldProperties['data'] - 1, $fieldProperties['operator'])); |
|
| 616 | - } |
|
| 617 | - $this->newFilter = true; |
|
| 618 | - } |
|
| 619 | - } else { // Zone texte |
|
| 620 | - if (trim($_REQUEST[$formFieldName]) != '') { |
|
| 621 | - $fieldProperties['data'] = $_REQUEST[$formFieldName]; |
|
| 622 | - if (!REFERENCES_EXACT_SEARCH) { |
|
| 623 | - $this->criteria->add(new Criteria($fieldName, '%' . $ts->addSlashes($fieldProperties['data']) . '%', 'LIKE')); |
|
| 624 | - } else { |
|
| 625 | - $this->criteria->add(new Criteria($fieldName, $ts->addSlashes($fieldProperties['data']) . '%', 'LIKE')); |
|
| 626 | - } |
|
| 627 | - $this->newFilter = true; |
|
| 628 | - } |
|
| 629 | - } |
|
| 630 | - } |
|
| 631 | - $this->vars[$fieldName] = $fieldProperties; |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - if ($this->newFilter) { |
|
| 635 | - $this->setStartInSession(0); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - // Récupération des donées de la session s'il n'y a pas eu de filtre(s) |
|
| 639 | - if (!$this->newFilter && isset($_SESSION[$this->getSessionName()])) { |
|
| 640 | - $sessionFilterData = unserialize($_SESSION[$this->getSessionName()]); |
|
| 641 | - if (isset($sessionFilterData['criteria']) && is_object($sessionFilterData['criteria'])) { |
|
| 642 | - $this->criteria = $sessionFilterData['criteria']; |
|
| 643 | - unset($sessionFilterData['criteria']); |
|
| 644 | - } |
|
| 645 | - foreach ($this->vars as $fieldName => $fieldProperties) { |
|
| 646 | - if (isset($sessionFilterData[$fieldName])) { |
|
| 647 | - $fieldProperties['data'] = $sessionFilterData[$fieldName]; |
|
| 648 | - } |
|
| 649 | - $this->vars[$fieldName] = $fieldProperties; |
|
| 650 | - } |
|
| 651 | - unset($_SESSION[$this->getSessionName()]); |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - // Mise en place des données dans la session |
|
| 655 | - $dataForSession = array(); |
|
| 656 | - $dataForSession['criteria'] = $this->criteria; |
|
| 657 | - foreach ($this->vars as $fieldName => $fieldProperties) { |
|
| 658 | - $dataForSession[$fieldName] = $fieldProperties['data']; |
|
| 659 | - } |
|
| 660 | - $_SESSION[$this->getSessionName()] = serialize($dataForSession); |
|
| 661 | - |
|
| 662 | - return $this; |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - /** |
|
| 666 | - * Retourne le nombre d'enregistrement en fonction des critères courants |
|
| 667 | - * |
|
| 668 | - * @return integer |
|
| 669 | - */ |
|
| 670 | - public function getCount() |
|
| 671 | - { |
|
| 672 | - if (!$this->isInitialized) { |
|
| 673 | - $this->filter(); |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - return $this->handler->getCount($this->criteria); |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - /** |
|
| 680 | - * Conserve la valeur de start en session |
|
| 681 | - * |
|
| 682 | - * @param integer $start |
|
| 683 | - * @return void |
|
| 684 | - */ |
|
| 685 | - private function setStartInSession($start) |
|
| 686 | - { |
|
| 687 | - if ($this->keepStart) { |
|
| 688 | - $startSessionName = $this->getStartSessionName(); |
|
| 689 | - $_SESSION[$startSessionName] = (int)$start; |
|
| 690 | - } |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - /** |
|
| 694 | - * Retourne la valeur de ?start=x |
|
| 695 | - * |
|
| 696 | - * @return integer |
|
| 697 | - */ |
|
| 698 | - private function getStartValue() |
|
| 699 | - { |
|
| 700 | - $start = 0; |
|
| 701 | - if (isset($_REQUEST[$this->startName])) { |
|
| 702 | - $start = (int)$_REQUEST[$this->startName]; |
|
| 703 | - } elseif ($this->keepStart) { |
|
| 704 | - $startSessionName = $this->getStartSessionName(); |
|
| 705 | - if (isset($_SESSION[$startSessionName])) { |
|
| 706 | - $start = (int)$_SESSION[$startSessionName]; |
|
| 707 | - } |
|
| 708 | - } |
|
| 709 | - // Mise en session |
|
| 710 | - $this->setStartInSession($start); |
|
| 711 | - |
|
| 712 | - return $start; |
|
| 713 | - } |
|
| 714 | - |
|
| 715 | - /** |
|
| 716 | - * Permet d'ajouter un paramètre supplémentaire au pager |
|
| 717 | - * |
|
| 718 | - * @param string $key |
|
| 719 | - * @param string $value |
|
| 720 | - * @return object |
|
| 721 | - */ |
|
| 722 | - public function addAdditionnalParameterToPager($key, $value = '') |
|
| 723 | - { |
|
| 724 | - $this->additionnalPagerParameters[$key] = $value; |
|
| 725 | - |
|
| 726 | - return $this; |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - /** |
|
| 730 | - * Permet d'ajouter un paramètre supplémentaire au bouton permettant de supprimer le filtre |
|
| 731 | - * |
|
| 732 | - * @param string $key |
|
| 733 | - * @param string $value |
|
| 734 | - * @return object |
|
| 735 | - */ |
|
| 736 | - public function addAdditionnalParameterToClearButton($key, $value = '') |
|
| 737 | - { |
|
| 738 | - $this->additionnalClearButtonParameters[$key] = $value; |
|
| 739 | - |
|
| 740 | - return $this; |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - /** |
|
| 744 | - * Permet d'ajouter des paramètres supplémentaires au pager |
|
| 745 | - * |
|
| 746 | - * @param $array |
|
| 747 | - * @return object |
|
| 748 | - * @internal param string $key |
|
| 749 | - * @internal param string $value |
|
| 750 | - */ |
|
| 751 | - public function addAditionnalArrayParametersToPager($array) |
|
| 752 | - { |
|
| 753 | - if (count($array) > 0) { |
|
| 754 | - foreach ($array as $key => $value) { |
|
| 755 | - $this->addAdditionnalParameterToPager($key, $value); |
|
| 756 | - } |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - return $this; |
|
| 760 | - } |
|
| 761 | - |
|
| 762 | - /** |
|
| 763 | - * Retourne le pager à utiliser |
|
| 764 | - * |
|
| 765 | - * @return mixed Null s'il n'y a pas lieu d'y avoir un pager, sinon un objet de type {@link XoopsPageNav} |
|
| 766 | - */ |
|
| 767 | - public function getPager() |
|
| 768 | - { |
|
| 769 | - if (!$this->isInitialized) { |
|
| 770 | - $this->filter(); |
|
| 771 | - } |
|
| 772 | - require_once XOOPS_ROOT_PATH . '/class/pagenav.php'; |
|
| 773 | - $itemsCount = $this->getCount(); |
|
| 774 | - $queryString = array(); |
|
| 775 | - if (trim($this->op) != '') { |
|
| 776 | - $queryString[$this->operationName] = $this->op; |
|
| 777 | - } |
|
| 778 | - $pagenav = null; |
|
| 779 | - |
|
| 780 | - if ($itemsCount > $this->limit) { |
|
| 781 | - foreach ($this->vars as $fieldName => $fieldProperties) { |
|
| 782 | - $formFieldName = self::PREFIX . $fieldName; // "filter_website_id" par exemple |
|
| 783 | - $queryString[$formFieldName] = $fieldProperties['data']; |
|
| 784 | - } |
|
| 785 | - // Ajout des paramètres supplémentaires éventuels |
|
| 786 | - if (count($this->additionnalPagerParameters) > 0) { |
|
| 787 | - foreach ($this->additionnalPagerParameters as $key => $value) { |
|
| 788 | - $queryString[$key] = $value; |
|
| 789 | - } |
|
| 790 | - } |
|
| 791 | - $start = $this->getStartValue(); |
|
| 792 | - $pagenav = new XoopsPageNav($itemsCount, $this->limit, $start, $this->startName, http_build_query($queryString)); |
|
| 793 | - } |
|
| 794 | - |
|
| 795 | - return $pagenav; |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - /** |
|
| 799 | - * Retourne une liste d'objets en fonction des critères définis |
|
| 800 | - * |
|
| 801 | - * @return array |
|
| 802 | - */ |
|
| 803 | - public function getObjects() |
|
| 804 | - { |
|
| 805 | - if (!$this->isInitialized) { |
|
| 806 | - $this->filter(); |
|
| 807 | - } |
|
| 808 | - $start = $this->getStartValue(); |
|
| 809 | - $limit = $this->limit; |
|
| 810 | - $criteria = $this->criteria; |
|
| 811 | - $criteria->setStart($start); |
|
| 812 | - $criteria->setLimit($limit); |
|
| 813 | - |
|
| 814 | - $criteria->setOrder($this->sortOrder); |
|
| 815 | - if (trim($this->sortField) != '') { |
|
| 816 | - $criteria->setSort($this->sortField); |
|
| 817 | - } elseif (trim($this->handler->identifierName) != '') { |
|
| 818 | - $criteria->setSort($this->handler->identifierName); |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - return $this->handler->getObjects($this->criteria); |
|
| 822 | - } |
|
| 823 | - |
|
| 824 | - /** |
|
| 825 | - * Retourne la zone html à utiliser pour créer la zone de filtre (avec sa valeur saisie si c'est le cas) |
|
| 826 | - * |
|
| 827 | - * @param string $fieldName La zone de saisie dont on veut récupérer le code html |
|
| 828 | - * @return string |
|
| 829 | - */ |
|
| 830 | - public function getFilterField($fieldName) |
|
| 831 | - { |
|
| 832 | - $html = ''; |
|
| 833 | - if (!$this->isInitialized) { |
|
| 834 | - $this->filter(); |
|
| 835 | - } |
|
| 836 | - if (!isset($this->vars[$fieldName])) { |
|
| 837 | - trigger_error('Error, unknow field'); |
|
| 838 | - |
|
| 839 | - return $html; |
|
| 840 | - } |
|
| 841 | - $fieldData = $this->vars[$fieldName]; |
|
| 842 | - $htmlFieldName = self::PREFIX . $fieldName; |
|
| 843 | - |
|
| 844 | - switch ($fieldData['fieldType']) { |
|
| 845 | - case self::FILTER_FIELD_TEXT: // Zone de texte |
|
| 846 | - $ts = MyTextSanitizer::getInstance(); |
|
| 847 | - $html = "<input type='text' name='$htmlFieldName' id='$htmlFieldName' size='" . $fieldData['size'] . "' maxlength='" . $fieldData['maxLength'] . "' value='" . $ts->htmlSpecialChars($fieldData['data']) . "' />"; |
|
| 848 | - break; |
|
| 849 | - |
|
| 850 | - case self::FILTER_FIELD_SELECT; // Select |
|
| 851 | - $style = ''; |
|
| 852 | - if (isset($fieldData['style']) && trim($fieldData['style']) != '') { |
|
| 853 | - $style = $fieldData['style']; |
|
| 854 | - } |
|
| 855 | - $html = references_utils::htmlSelect($htmlFieldName, $fieldData['values'], $fieldData['data'], $fieldData['withNull'], $style); |
|
| 856 | - break; |
|
| 857 | - |
|
| 858 | - case self::FILTER_FIELD_SELECT_YES_NO: // Select de type Oui/Non |
|
| 859 | - $html = references_utils::htmlSelect($htmlFieldName, array(2 => _YES, 1 => NO), $fieldData['data'], $fieldData['withNull']); |
|
| 860 | - break; |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - return $html; |
|
| 864 | - } |
|
| 865 | - |
|
| 866 | - /** |
|
| 867 | - * Assigne toutes les zones de filtre à un template |
|
| 868 | - * |
|
| 869 | - * @param object $xoopsTpl |
|
| 870 | - * @param boolean $asArray Est-ce qu'il faut placer le résultat dans un tableau ou assigner par nom de zone de filtre |
|
| 871 | - * @param string $arrayName Le nom du tableau à utiliser |
|
| 872 | - * @return void |
|
| 873 | - */ |
|
| 874 | - public function assignFilterFieldsToTemplate(&$xoopsTpl, $asArray = true, $arrayName = 'filterFields') |
|
| 875 | - { |
|
| 876 | - $fields = array_keys($this->vars); |
|
| 877 | - foreach ($fields as $field) { |
|
| 878 | - if (!$asArray) { |
|
| 879 | - $xoopsTpl->assign($field, $this->getFilterField($field)); |
|
| 880 | - } else { |
|
| 881 | - $xoopsTpl->append($arrayName, $this->getFilterField($field)); |
|
| 882 | - } |
|
| 883 | - } |
|
| 884 | - } |
|
| 885 | - |
|
| 886 | - /** |
|
| 887 | - * Retourne le bouton utilisé pour supprimer le filtre en cours |
|
| 888 | - * |
|
| 889 | - * @return string |
|
| 890 | - */ |
|
| 891 | - public function getClearFilterbutton() |
|
| 892 | - { |
|
| 893 | - $queryString = array(); |
|
| 894 | - $queryString[$this->operationName] = $this->op; |
|
| 895 | - if (count($this->additionnalClearButtonParameters) > 0) { |
|
| 896 | - foreach ($this->additionnalClearButtonParameters as $key => $value) { |
|
| 897 | - $queryString[$key] = $value; |
|
| 898 | - } |
|
| 899 | - } |
|
| 900 | - $queryString['cleanFilter'] = '1'; |
|
| 901 | - $baseurl = $this->baseUrl; |
|
| 902 | - |
|
| 903 | - return " <a href='$baseurl?" . http_build_query($queryString) . "' title='" . _MD_REFERENCES_CLEAN_FILTER . "'><img align='top' src='../assets/images/clear_left.png' alt='" . _MD_REFERENCES_CLEAN_FILTER . "' /></a>"; |
|
| 904 | - } |
|
| 905 | - |
|
| 906 | - /** |
|
| 907 | - * Retourne le bouton permettant de lancer le filtrage |
|
| 908 | - * |
|
| 909 | - * @param string $description Texte à faire apparaître sur le bouton |
|
| 910 | - * @param array $additionnals Champs supplémentaires à faire apparaître avec le bouton |
|
| 911 | - * @return string |
|
| 912 | - */ |
|
| 913 | - public function getGoButton($description = _GO, $additionnals = null) |
|
| 914 | - { |
|
| 915 | - $html = ''; |
|
| 916 | - if (trim($this->operationName) != '' && trim($this->op) != '') { |
|
| 917 | - $html .= "<input type='hidden' name='" . $this->operationName . "' id='" . $this->operationName . "' value='" . $this->op . "' />"; |
|
| 918 | - } |
|
| 919 | - if (!is_null($additionnals)) { |
|
| 920 | - foreach ($additionnals as $key => $value) { |
|
| 921 | - $html .= "<input type='hidden' name='" . $key . "' id='" . $key . "' value='" . $value . "' />"; |
|
| 922 | - } |
|
| 923 | - } |
|
| 924 | - $html .= "<input type='submit' name='btngo' id='btngo' value='$description' />"; |
|
| 925 | - |
|
| 926 | - return $html; |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - /** |
|
| 930 | - * Retourne le nom de la zone utilisée pour trier les données |
|
| 931 | - * |
|
| 932 | - * @return string |
|
| 933 | - */ |
|
| 934 | - public function getSortField() |
|
| 935 | - { |
|
| 936 | - return $this->sortField; |
|
| 937 | - } |
|
| 938 | - |
|
| 939 | - /** |
|
| 940 | - * Retourne le sens de tri utilisé pour trier les données |
|
| 941 | - * |
|
| 942 | - * @return string |
|
| 943 | - */ |
|
| 944 | - public function getSortOrder() |
|
| 945 | - { |
|
| 946 | - return $this->sortOrder(); |
|
| 947 | - } |
|
| 948 | - |
|
| 949 | - /** |
|
| 950 | - * Retourne la valeur d'un champ |
|
| 951 | - * |
|
| 952 | - * @param string $fieldName |
|
| 953 | - * @return mixed La valeur du champ ou null si on ne trouve pas la zone |
|
| 954 | - */ |
|
| 955 | - public function getFieldValue($fieldName) |
|
| 956 | - { |
|
| 957 | - $ret = null; |
|
| 958 | - if (isset($this->vars[$fieldName])) { |
|
| 959 | - $ret = $this->vars[$fieldName]['data']; |
|
| 960 | - } |
|
| 961 | - |
|
| 962 | - return $ret; |
|
| 963 | - } |
|
| 38 | + const FILTER_DATA_TEXT = 1; |
|
| 39 | + const FILTER_DATA_NUMERIC = 2; |
|
| 40 | + |
|
| 41 | + const FILTER_FIELD_TEXT = 1; |
|
| 42 | + const FILTER_FIELD_SELECT = 2; |
|
| 43 | + const FILTER_FIELD_SELECT_YES_NO = 3; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Préfixe des variables de sélection |
|
| 47 | + */ |
|
| 48 | + const PREFIX = 'filter_'; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Nom du module (utilisé pour la session) |
|
| 52 | + */ |
|
| 53 | + const MODULE_NAME = 'references'; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Contient toutes les variables participant au filtre (avec leur type et description) |
|
| 57 | + * |
|
| 58 | + * @var array |
|
| 59 | + */ |
|
| 60 | + private $vars = array(); |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Handler des données |
|
| 64 | + * |
|
| 65 | + * @var reference |
|
| 66 | + */ |
|
| 67 | + private $handler = null; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Nom de la zone d'opération (par exemple op=products) |
|
| 71 | + * |
|
| 72 | + * @var string |
|
| 73 | + */ |
|
| 74 | + private $op = ''; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * action de l'opération en cour (par exemple op=products) |
|
| 78 | + * |
|
| 79 | + * @var string |
|
| 80 | + */ |
|
| 81 | + private $operationName = ''; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Nombre maximum d'éléments pas page |
|
| 85 | + * |
|
| 86 | + * @var intger |
|
| 87 | + */ |
|
| 88 | + private $limit = 10; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Nom de la variable start |
|
| 92 | + * |
|
| 93 | + * @var string |
|
| 94 | + */ |
|
| 95 | + private $startName = ''; |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Critère {@link Criteria} servant à choisir les données |
|
| 99 | + * |
|
| 100 | + * @var object |
|
| 101 | + */ |
|
| 102 | + private $criteria = null; |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Indique s'il y a un nouveau critère |
|
| 106 | + * |
|
| 107 | + * @var boolean |
|
| 108 | + */ |
|
| 109 | + private $newFilter = false; |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Indique si la méthode filter() a été appelée |
|
| 113 | + * |
|
| 114 | + * @var boolean |
|
| 115 | + */ |
|
| 116 | + private $isInitialized = false; |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Zone de tri pour retourner les données (dans getObjects) |
|
| 120 | + * |
|
| 121 | + * @var string |
|
| 122 | + */ |
|
| 123 | + private $sortField = ''; |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Sens de tri |
|
| 127 | + * |
|
| 128 | + * @var string |
|
| 129 | + */ |
|
| 130 | + private $sortOrder = ''; |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Liste de critères par défaut |
|
| 134 | + * |
|
| 135 | + * @var array |
|
| 136 | + */ |
|
| 137 | + private $defaultCriterias = array(); |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * L'url complète du script qui appelle |
|
| 141 | + * |
|
| 142 | + * @var string |
|
| 143 | + */ |
|
| 144 | + private $baseUrl = ''; |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Indique si on conserve en session la position de départ |
|
| 148 | + * |
|
| 149 | + * @var boolean |
|
| 150 | + */ |
|
| 151 | + private $keepStart = true; |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Indique si l'autocomplétion est activée |
|
| 155 | + * |
|
| 156 | + * @var boolean |
|
| 157 | + */ |
|
| 158 | + private $hasAutoComplete = false; |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Url du dossier js dans le module |
|
| 162 | + * |
|
| 163 | + * @var string |
|
| 164 | + */ |
|
| 165 | + private $jsFolderUrl = ''; |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Paramètres additionnels à ajouter aux paramètres du pager |
|
| 169 | + * @var array |
|
| 170 | + */ |
|
| 171 | + private $additionnalPagerParameters = array(); |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Paramètres additionnels à ajouter au bouton permettant de supprimer le filtre en cours |
|
| 175 | + * @var array |
|
| 176 | + */ |
|
| 177 | + private $additionnalClearButtonParameters = array(); |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Tableau des champs triables [clé] = nom du champ dans la base, valeur = libellé |
|
| 181 | + * @var array |
|
| 182 | + */ |
|
| 183 | + private $sortFields = array(); |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Initialise les paramètres avec des valeurs par défaut |
|
| 187 | + */ |
|
| 188 | + private function setDefaultValues() |
|
| 189 | + { |
|
| 190 | + $this->handler = null; |
|
| 191 | + $this->op = ''; |
|
| 192 | + $this->limit = 10; |
|
| 193 | + $this->startName = 'start'; |
|
| 194 | + $this->operationName = 'op'; |
|
| 195 | + $this->sortField = ''; |
|
| 196 | + $this->sortOrder = 'asc'; |
|
| 197 | + $this->baseUrl = ''; |
|
| 198 | + $this->keepStart = true; |
|
| 199 | + $this->hasAutoComplete = false; |
|
| 200 | + $this->jsFolderUrl = ''; |
|
| 201 | + $this->additionnalPagerParameters = array(); |
|
| 202 | + $this->additionnalClearButtonParameters = array(); |
|
| 203 | + $this->sortFields = array(); // Les champs qui peuvent être utilisés pour trier |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Initialisation des données, handler et opération courante dans l'appelant |
|
| 208 | + * |
|
| 209 | + * @param mixed $handler Soit une référence au handler de données soit un tableau qui contient toutes les options (auxquel cas les autres paramètres sont inutiles) |
|
| 210 | + * @param string $operationName |
|
| 211 | + * @param string $operation Opération courante dans l'appelant |
|
| 212 | + * @param string $startName Nom du paramètre start |
|
| 213 | + * @param integer $limit Nombre maximum d'éléments par page |
|
| 214 | + * @param string $baseUrl L'url complète du script appelant |
|
| 215 | + * @param string $sortField Zone de tri |
|
| 216 | + * @param string $sortOrder Sens de tri |
|
| 217 | + * @param boolean $keepStart Indique si on conserve la position de départ |
|
| 218 | + * @param string $jsFolderUrl |
|
| 219 | + * @package string $jsFolderUrl Url du répertoire qui contient les scripts Javascript |
|
| 220 | + */ |
|
| 221 | + public function __construct($handler, $operationName = 'op', $operation = '', $startName = 'start', $limit = 10, $baseUrl = '', $sortField = '', $sortOrder = 'asc', $keepStart = true, $jsFolderUrl = '') |
|
| 222 | + { |
|
| 223 | + $this->setDefaultValues(); |
|
| 224 | + if (!is_array($handler)) { |
|
| 225 | + $this->handler = $handler; |
|
| 226 | + $this->op = $operation; |
|
| 227 | + $this->limit = $limit; |
|
| 228 | + $this->startName = $startName; |
|
| 229 | + $this->operationName = $operationName; |
|
| 230 | + $this->sortField = $sortField; |
|
| 231 | + $this->sortOrder = $sortOrder; |
|
| 232 | + $this->baseUrl = $baseUrl; |
|
| 233 | + $this->keepStart = $keepStart; |
|
| 234 | + $this->jsFolderUrl = $jsFolderUrl; |
|
| 235 | + } else { |
|
| 236 | + foreach ($handler as $key => $value) { |
|
| 237 | + $this->$key = $value; |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Donne à la classe le nom des champs sur lesquels on peut faire le tri |
|
| 244 | + * |
|
| 245 | + * @param array $fields [clé] = nom du champ dans la base, valeur = libellé |
|
| 246 | + * @return object |
|
| 247 | + */ |
|
| 248 | + public function setSortFields($fields) |
|
| 249 | + { |
|
| 250 | + $this->sortFields = $fields; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Retourne les noms à utiliser pour les champs de tri (sélecteur de champ et ordre de tri) |
|
| 255 | + * |
|
| 256 | + * @return array [0] = Nom du sélecteur de champs, [1] = Nom du sélecteur pour le sens du tri |
|
| 257 | + */ |
|
| 258 | + private function getSortPlaceHolderNames() |
|
| 259 | + { |
|
| 260 | + return array(self::PREFIX . 'sortFields', self::PREFIX . 'sortOrder'); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Retourne 2 sélecteurs html pour choisir la zone de tri et le sens du tri |
|
| 265 | + * |
|
| 266 | + * @return string |
|
| 267 | + */ |
|
| 268 | + public function getSortPlaceHolderHtmlCode() |
|
| 269 | + { |
|
| 270 | + $sortNames = $this->getSortPlaceHolderNames(); |
|
| 271 | + $sortFieldsHtml = references_utils::htmlSelect($sortNames[0], $this->sortFields, $this->sortField, false); |
|
| 272 | + $sortOrderHtml = references_utils::htmlSelect($sortNames[1], array('asc' => _MD_REFERENCES_ASC, 'desc' => _MD_REFERENCES_DESC), $this->sortOrder, false); |
|
| 273 | + |
|
| 274 | + return _MD_REFERENCES_SORT_BY . ' ' . $sortFieldsHtml . ' ' . $sortOrderHtml; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Permet de valoriser une option directement tout en chainant |
|
| 279 | + * |
|
| 280 | + * @param string $optionName |
|
| 281 | + * @param mixed $optionValue |
|
| 282 | + * @return object |
|
| 283 | + */ |
|
| 284 | + public function setOption($optionName, $optionValue) |
|
| 285 | + { |
|
| 286 | + $this->$optionName = $optionValue; |
|
| 287 | + |
|
| 288 | + return $this; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Ajoute un nouveau critère par défaut à la liste des critères par défaut |
|
| 293 | + * |
|
| 294 | + * @param Criteria $criteria |
|
| 295 | + */ |
|
| 296 | + public function addDefaultCriteria(Criteria $criteria) |
|
| 297 | + { |
|
| 298 | + $this->defaultCriterias[] = $criteria; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Retourne une valeur d'un tableau ou null si l'index n'existe pas |
|
| 303 | + * |
|
| 304 | + * @param array $array Le tableau à traiter |
|
| 305 | + * @param string $index L'index recherché |
|
| 306 | + * @param mixed $defaultValue La valeur par défaut |
|
| 307 | + * @return mixed |
|
| 308 | + */ |
|
| 309 | + private function getArrayValue($array, $index, $defaultValue = false) |
|
| 310 | + { |
|
| 311 | + if ($index === 'autoComplete' && isset($array[$index]) && isset($array[$index]) == true) { |
|
| 312 | + $this->hasAutoComplete = true; // On en profite pour vérifier si un champ utilise l'autocomplétion |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + return isset($array[$index]) ? $array[$index] : $defaultValue; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Permet de faire l'autocomplétion d'un champ |
|
| 320 | + * |
|
| 321 | + * @param string $query |
|
| 322 | + * @param $limit |
|
| 323 | + * @param string $fieldName |
|
| 324 | + * @return string |
|
| 325 | + */ |
|
| 326 | + public function autoComplete($query, $limit, $fieldName) |
|
| 327 | + { |
|
| 328 | + $return = ''; |
|
| 329 | + if (!$this->hasAutoComplete) { // Si aucun champ n'est en autocomplétion, c'est pas la peine d'aller plus loin |
|
| 330 | + |
|
| 331 | + return $return; |
|
| 332 | + } |
|
| 333 | + if (isset($this->vars[$fieldName])) { // On vérifie que le champ demandé est bien en autocomplétion |
|
| 334 | + if ($this->vars[$fieldName]['autoComplete'] == true) { |
|
| 335 | + if ($this->vars[$fieldName]['dataType'] == self::FILTER_DATA_TEXT) { |
|
| 336 | + $criteria = new Criteria($fieldName, $query . '%', 'LIKE'); |
|
| 337 | + } |
|
| 338 | + $criteria->setLimit((int)$limit); |
|
| 339 | + $ret = $this->handler->getObjects($criteria); |
|
| 340 | + |
|
| 341 | + if (count($ret) > 0) { |
|
| 342 | + foreach ($ret as $object) { |
|
| 343 | + $return .= $object->getVar($fieldName, 'n') . "\n"; |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + } |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + return $return; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Retourne le code Javascript à utiliser pour initialiser l'auto complétion (et donc à coller dans le code html) |
|
| 354 | + * |
|
| 355 | + * @param boolean $jqueryAlreadyLoaded Indique si jQuery est déjà chargé par l'appelant, auquel cas rien ne sert de le recharger |
|
| 356 | + * @return string |
|
| 357 | + */ |
|
| 358 | + public function getJavascriptInitCode($jqueryAlreadyLoaded = false) |
|
| 359 | + { |
|
| 360 | + $return = ''; |
|
| 361 | + if (!$this->hasAutoComplete) { |
|
| 362 | + return $return; |
|
| 363 | + } |
|
| 364 | + $return = ''; |
|
| 365 | + $return .= "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" title=\"Style sheet\" href=\"" . $this->jsFolderUrl . "autocomplete/jquery.autocomplete.css\" />\n"; |
|
| 366 | + if (!$jqueryAlreadyLoaded) { |
|
| 367 | + $return .= "<script type=\"text/javascript\" src=\"" . $this->jsFolderUrl . "jquery/jquery.js\"></script>\n"; |
|
| 368 | + } |
|
| 369 | + $return .= "<script type=\"text/javascript\" src=\"" . $this->jsFolderUrl . "noconflict.js\"></script>\n"; |
|
| 370 | + $return .= "<script type=\"text/javascript\" src=\"" . $this->jsFolderUrl . "autocomplete/jquery.autocomplete.min.js\"></script>\n"; |
|
| 371 | + $return .= "<script type=\"text/javascript\">\n"; |
|
| 372 | + $return .= "jQuery(function($) {\n"; |
|
| 373 | + $return .= "var url='" . $this->baseUrl . "';\n"; // TODO: Supprimer "var" car cela limite sa portée ! |
|
| 374 | + $return .= "var handlerName='" . $this->handler->className . "';\n"; |
|
| 375 | + |
|
| 376 | + foreach ($this->vars as $fieldName => $parameters) { |
|
| 377 | + if ($parameters['autoComplete'] == true) { |
|
| 378 | + $field = self::PREFIX . $fieldName; |
|
| 379 | + $return .= "$('#" . $field . "').autocomplete(url, {\n"; |
|
| 380 | + $return .= "extraParams: {\n"; |
|
| 381 | + $return .= " field: '" . $fieldName . "',\n"; |
|
| 382 | + $return .= " op: 'autocomplete',\n"; |
|
| 383 | + $return .= " handler: handlerName\n"; |
|
| 384 | + $return .= "}\n"; |
|
| 385 | + $return .= "});\n"; |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + $return .= "});\n"; |
|
| 389 | + $return .= "</script>\n"; |
|
| 390 | + |
|
| 391 | + return $return; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * Initialisation des données du filtre |
|
| 396 | + * Permet d'indiquer quelles sont les zones sur lesquelles on effectue des filtres ainsi que leur type |
|
| 397 | + * |
|
| 398 | + * @param string $fieldName Le nom du champs dans la table |
|
| 399 | + * @param array $parameters Les paramètres de la zone sous la forme d'un tableau : |
|
| 400 | + * [dataType] Entier représentant le type de donnée (numérique ou chaine) |
|
| 401 | + * [fieldType] Le type de zone de saisie (zone de texte, liste déroulante, liste déroulante Oui/Non) |
|
| 402 | + * [values] La ou les valeurs de la zone de saisie (utilisé dans le cas d'un select) |
|
| 403 | + * [size] Largeur d'affichage pour les textbox |
|
| 404 | + * [maxLength] Largeur maximale pour les textbox |
|
| 405 | + * [withNull] Dans le cas des listes déroulantes, indique s'il faut une valeur nulle |
|
| 406 | + * [minusOne] Indique s'il faut retrancher 1 à la valeur saisie récupérée (cas classique des listes Oui/Non avec une valeur nulle) |
|
| 407 | + * [style] Dans le cas des liste déroulante, le style à appliquer à la liste |
|
| 408 | + * [data] A ne pas renseigner, contient la valeur saisie par l'utilisateur |
|
| 409 | + * [operator] Opérateur de comparaison pour le Criteria |
|
| 410 | + * [autoComplete] Indique si on active l'auto complétion sur le champs |
|
| 411 | + * @return object L'objet courant pour pouvoir chainer |
|
| 412 | + */ |
|
| 413 | + public function initFilter($fieldName, $parameters) |
|
| 414 | + { |
|
| 415 | + // Tableau des valeurs attendues avec leur valeur par défaut |
|
| 416 | + $indexNames = array('dataType' => self::FILTER_DATA_TEXT, 'fieldType' => self::FILTER_FIELD_TEXT, 'values' => null, 'withNull' => false, 'size' => 5, 'maxLength' => 255, 'minusOne' => false, 'data' => null, 'style' => '', 'operator' => '=', 'autoComplete' => false); |
|
| 417 | + $data = array(); |
|
| 418 | + foreach ($indexNames as $indexName => $defaultValue) { |
|
| 419 | + $data[$indexName] = $this->getArrayValue($parameters, $indexName, $defaultValue); |
|
| 420 | + } |
|
| 421 | + $this->vars[$fieldName] = $data; |
|
| 422 | + |
|
| 423 | + return $this; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Retourne le nom du tableau à utiliser pour la session |
|
| 428 | + * @note : Le nom de la session est composé de : nom du module_nom du handler, par exemple references_references_articles |
|
| 429 | + * |
|
| 430 | + * @return string |
|
| 431 | + */ |
|
| 432 | + private function getSessionName() |
|
| 433 | + { |
|
| 434 | + return self::MODULE_NAME . '_' . $this->handler->table; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Retourne le nom de la clé à utiliser pour la conservation du start en session |
|
| 439 | + * |
|
| 440 | + * @return string |
|
| 441 | + */ |
|
| 442 | + private function getStartSessionName() |
|
| 443 | + { |
|
| 444 | + return $this->getSessionName() . '_start'; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * Réinitialisation des données avant traitement |
|
| 449 | + * |
|
| 450 | + * @return void |
|
| 451 | + */ |
|
| 452 | + private function reinitializeFieldsValue() |
|
| 453 | + { |
|
| 454 | + foreach ($this->vars as $fieldName => $fieldProperties) { |
|
| 455 | + if ($fieldProperties['dataType'] == self::FILTER_DATA_NUMERIC) { // Zone numérique |
|
| 456 | + $fieldProperties['data'] = 0; |
|
| 457 | + } else { |
|
| 458 | + $fieldProperties['data'] = ''; |
|
| 459 | + } |
|
| 460 | + $this->vars[$fieldName] = $fieldProperties; |
|
| 461 | + } |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Ajoute les critères par défaut au critère général |
|
| 466 | + * |
|
| 467 | + * @return void |
|
| 468 | + */ |
|
| 469 | + private function addDefaultCriterias() |
|
| 470 | + { |
|
| 471 | + if (is_array($this->defaultCriterias) && count($this->defaultCriterias) > 0) { |
|
| 472 | + foreach ($this->defaultCriterias as $criteria) { |
|
| 473 | + $this->criteria->add($criteria); |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + return $this; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + /** |
|
| 481 | + * Indique s'il y a des champs de tri |
|
| 482 | + * |
|
| 483 | + * @return boolean |
|
| 484 | + */ |
|
| 485 | + private function areThereSortFields() |
|
| 486 | + { |
|
| 487 | + $return = false; |
|
| 488 | + if (is_array($this->sortFields) && count($this->sortFields) > 0) { |
|
| 489 | + $return = true; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + return $return; |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + /** |
|
| 496 | + * Indique si le nom du champ passé en paramètre fait partie de la liste des champs "triables" |
|
| 497 | + * |
|
| 498 | + * @param string $fieldName |
|
| 499 | + * @return boolean |
|
| 500 | + */ |
|
| 501 | + private function isInSortFieldsList($fieldName) |
|
| 502 | + { |
|
| 503 | + return array_key_exists($fieldName, $this->sortFields); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * Indique si le sens de tri passé en paramètre fait partie de la liste autorisée |
|
| 508 | + * |
|
| 509 | + * @param string $order |
|
| 510 | + * @return boolean |
|
| 511 | + */ |
|
| 512 | + private function isInSortOrderList($order) |
|
| 513 | + { |
|
| 514 | + return in_array($order, array('asc', 'desc')); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + /** |
|
| 518 | + * Récupère, depuis la requête d'entrée, la zone de tri à utiliser et le sens du tri et place l'information en cookie |
|
| 519 | + * pour que lorsque l'utilisateur se reconnecte, il retrouve ses informations de tri |
|
| 520 | + * |
|
| 521 | + * @return void |
|
| 522 | + */ |
|
| 523 | + private function setSortFieldsFromRequest() |
|
| 524 | + { |
|
| 525 | + if (!$this->areThereSortFields()) { // S'il n'y a pas de champs triables, on laisse tomber |
|
| 526 | + |
|
| 527 | + return; |
|
| 528 | + } |
|
| 529 | + $sortField = $sortOrder = ''; |
|
| 530 | + $cookieName = $this->getSessionName(); |
|
| 531 | + $orderFieldsNames = $this->getSortPlaceHolderNames(); |
|
| 532 | + if (isset($_REQUEST[$orderFieldsNames[0]]) && isset($_REQUEST[$orderFieldsNames[1]])) { |
|
| 533 | + $sortField = $_REQUEST[$orderFieldsNames[0]]; |
|
| 534 | + $sortOrder = $_REQUEST[$orderFieldsNames[1]]; |
|
| 535 | + } else { |
|
| 536 | + if (isset($_SESSION[$cookieName . '_sortField'])) { |
|
| 537 | + $sortField = $_SESSION[$cookieName . '_sortField']; |
|
| 538 | + } |
|
| 539 | + if (isset($_SESSION[$cookieName . '_sortOrder'])) { |
|
| 540 | + $sortOrder = $_SESSION[$cookieName . '_sortOrder']; |
|
| 541 | + } |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + if ($this->isInSortFieldsList($sortField) && $this->isInSortOrderList($sortOrder)) { |
|
| 545 | + $this->sortField = $sortField; |
|
| 546 | + $this->sortOrder = $sortOrder; |
|
| 547 | + } |
|
| 548 | + if (trim($sortField) != '' && trim($sortOrder) != '') { |
|
| 549 | + $_SESSION[$cookieName . '_sortField'] = $sortField; |
|
| 550 | + $_SESSION[$cookieName . '_sortOrder'] = $sortOrder; |
|
| 551 | + } |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * Réinitialisation des données avant traitement |
|
| 556 | + * |
|
| 557 | + * @return void |
|
| 558 | + */ |
|
| 559 | + private function setupFilter() |
|
| 560 | + { |
|
| 561 | + $this->reinitializeFieldsValue(); |
|
| 562 | + $this->newFilter = false; |
|
| 563 | + $this->isInitialized = true; |
|
| 564 | + $this->criteria = new CriteriaCompo(); |
|
| 565 | + $this->criteria->add(new Criteria($this->handler->keyName, 0, '<>')); |
|
| 566 | + $this->addDefaultCriterias(); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * RAZ des données du filtre si cela a été demandé dans la requête |
|
| 571 | + * |
|
| 572 | + * @return void |
|
| 573 | + */ |
|
| 574 | + private function isSetCleanFilter() |
|
| 575 | + { |
|
| 576 | + if (isset($_REQUEST['cleanFilter'])) { |
|
| 577 | + $this->setStartInSession(0); |
|
| 578 | + unset($_SESSION[$this->getSessionName()]); |
|
| 579 | + } |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * Retourne le critère de filtrage courant |
|
| 584 | + * |
|
| 585 | + * @return object |
|
| 586 | + */ |
|
| 587 | + public function getCriteria() |
|
| 588 | + { |
|
| 589 | + return $this->criteria; |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + /** |
|
| 593 | + * Méthode à appeler juste après le constructeur pour qu'elle récupère les données saisies |
|
| 594 | + * |
|
| 595 | + * @return object L'objet courant pour pouvoir chainer |
|
| 596 | + */ |
|
| 597 | + public function filter() |
|
| 598 | + { |
|
| 599 | + $this->setupFilter(); // Réinitialisations |
|
| 600 | + $ts = MyTextSanitizer::getInstance(); |
|
| 601 | + $this->setSortFieldsFromRequest(); // On récupère la zone de tri éventuellement passée dans la requête |
|
| 602 | + $this->isSetCleanFilter(); |
|
| 603 | + |
|
| 604 | + foreach ($this->vars as $fieldName => $fieldProperties) { |
|
| 605 | + // On commence par récupérer toutes les valeurs |
|
| 606 | + $formFieldName = self::PREFIX . $fieldName; // "filter_website_id" par exemple |
|
| 607 | + $fieldProperties['data'] = null; // Valeur par défaut |
|
| 608 | + if (isset($_REQUEST[$formFieldName])) { |
|
| 609 | + if ($fieldProperties['dataType'] == self::FILTER_DATA_NUMERIC) { // Zone numérique |
|
| 610 | + if ((int)$_REQUEST[$formFieldName] != 0) { |
|
| 611 | + $fieldProperties['data'] = (int)$_REQUEST[$formFieldName]; |
|
| 612 | + if (!$fieldProperties['minusOne']) { |
|
| 613 | + $this->criteria->add(new Criteria($fieldName, $fieldProperties['data'], $fieldProperties['operator'])); |
|
| 614 | + } else { |
|
| 615 | + $this->criteria->add(new Criteria($fieldName, $fieldProperties['data'] - 1, $fieldProperties['operator'])); |
|
| 616 | + } |
|
| 617 | + $this->newFilter = true; |
|
| 618 | + } |
|
| 619 | + } else { // Zone texte |
|
| 620 | + if (trim($_REQUEST[$formFieldName]) != '') { |
|
| 621 | + $fieldProperties['data'] = $_REQUEST[$formFieldName]; |
|
| 622 | + if (!REFERENCES_EXACT_SEARCH) { |
|
| 623 | + $this->criteria->add(new Criteria($fieldName, '%' . $ts->addSlashes($fieldProperties['data']) . '%', 'LIKE')); |
|
| 624 | + } else { |
|
| 625 | + $this->criteria->add(new Criteria($fieldName, $ts->addSlashes($fieldProperties['data']) . '%', 'LIKE')); |
|
| 626 | + } |
|
| 627 | + $this->newFilter = true; |
|
| 628 | + } |
|
| 629 | + } |
|
| 630 | + } |
|
| 631 | + $this->vars[$fieldName] = $fieldProperties; |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + if ($this->newFilter) { |
|
| 635 | + $this->setStartInSession(0); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + // Récupération des donées de la session s'il n'y a pas eu de filtre(s) |
|
| 639 | + if (!$this->newFilter && isset($_SESSION[$this->getSessionName()])) { |
|
| 640 | + $sessionFilterData = unserialize($_SESSION[$this->getSessionName()]); |
|
| 641 | + if (isset($sessionFilterData['criteria']) && is_object($sessionFilterData['criteria'])) { |
|
| 642 | + $this->criteria = $sessionFilterData['criteria']; |
|
| 643 | + unset($sessionFilterData['criteria']); |
|
| 644 | + } |
|
| 645 | + foreach ($this->vars as $fieldName => $fieldProperties) { |
|
| 646 | + if (isset($sessionFilterData[$fieldName])) { |
|
| 647 | + $fieldProperties['data'] = $sessionFilterData[$fieldName]; |
|
| 648 | + } |
|
| 649 | + $this->vars[$fieldName] = $fieldProperties; |
|
| 650 | + } |
|
| 651 | + unset($_SESSION[$this->getSessionName()]); |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + // Mise en place des données dans la session |
|
| 655 | + $dataForSession = array(); |
|
| 656 | + $dataForSession['criteria'] = $this->criteria; |
|
| 657 | + foreach ($this->vars as $fieldName => $fieldProperties) { |
|
| 658 | + $dataForSession[$fieldName] = $fieldProperties['data']; |
|
| 659 | + } |
|
| 660 | + $_SESSION[$this->getSessionName()] = serialize($dataForSession); |
|
| 661 | + |
|
| 662 | + return $this; |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + /** |
|
| 666 | + * Retourne le nombre d'enregistrement en fonction des critères courants |
|
| 667 | + * |
|
| 668 | + * @return integer |
|
| 669 | + */ |
|
| 670 | + public function getCount() |
|
| 671 | + { |
|
| 672 | + if (!$this->isInitialized) { |
|
| 673 | + $this->filter(); |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + return $this->handler->getCount($this->criteria); |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + /** |
|
| 680 | + * Conserve la valeur de start en session |
|
| 681 | + * |
|
| 682 | + * @param integer $start |
|
| 683 | + * @return void |
|
| 684 | + */ |
|
| 685 | + private function setStartInSession($start) |
|
| 686 | + { |
|
| 687 | + if ($this->keepStart) { |
|
| 688 | + $startSessionName = $this->getStartSessionName(); |
|
| 689 | + $_SESSION[$startSessionName] = (int)$start; |
|
| 690 | + } |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + /** |
|
| 694 | + * Retourne la valeur de ?start=x |
|
| 695 | + * |
|
| 696 | + * @return integer |
|
| 697 | + */ |
|
| 698 | + private function getStartValue() |
|
| 699 | + { |
|
| 700 | + $start = 0; |
|
| 701 | + if (isset($_REQUEST[$this->startName])) { |
|
| 702 | + $start = (int)$_REQUEST[$this->startName]; |
|
| 703 | + } elseif ($this->keepStart) { |
|
| 704 | + $startSessionName = $this->getStartSessionName(); |
|
| 705 | + if (isset($_SESSION[$startSessionName])) { |
|
| 706 | + $start = (int)$_SESSION[$startSessionName]; |
|
| 707 | + } |
|
| 708 | + } |
|
| 709 | + // Mise en session |
|
| 710 | + $this->setStartInSession($start); |
|
| 711 | + |
|
| 712 | + return $start; |
|
| 713 | + } |
|
| 714 | + |
|
| 715 | + /** |
|
| 716 | + * Permet d'ajouter un paramètre supplémentaire au pager |
|
| 717 | + * |
|
| 718 | + * @param string $key |
|
| 719 | + * @param string $value |
|
| 720 | + * @return object |
|
| 721 | + */ |
|
| 722 | + public function addAdditionnalParameterToPager($key, $value = '') |
|
| 723 | + { |
|
| 724 | + $this->additionnalPagerParameters[$key] = $value; |
|
| 725 | + |
|
| 726 | + return $this; |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + /** |
|
| 730 | + * Permet d'ajouter un paramètre supplémentaire au bouton permettant de supprimer le filtre |
|
| 731 | + * |
|
| 732 | + * @param string $key |
|
| 733 | + * @param string $value |
|
| 734 | + * @return object |
|
| 735 | + */ |
|
| 736 | + public function addAdditionnalParameterToClearButton($key, $value = '') |
|
| 737 | + { |
|
| 738 | + $this->additionnalClearButtonParameters[$key] = $value; |
|
| 739 | + |
|
| 740 | + return $this; |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + /** |
|
| 744 | + * Permet d'ajouter des paramètres supplémentaires au pager |
|
| 745 | + * |
|
| 746 | + * @param $array |
|
| 747 | + * @return object |
|
| 748 | + * @internal param string $key |
|
| 749 | + * @internal param string $value |
|
| 750 | + */ |
|
| 751 | + public function addAditionnalArrayParametersToPager($array) |
|
| 752 | + { |
|
| 753 | + if (count($array) > 0) { |
|
| 754 | + foreach ($array as $key => $value) { |
|
| 755 | + $this->addAdditionnalParameterToPager($key, $value); |
|
| 756 | + } |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + return $this; |
|
| 760 | + } |
|
| 761 | + |
|
| 762 | + /** |
|
| 763 | + * Retourne le pager à utiliser |
|
| 764 | + * |
|
| 765 | + * @return mixed Null s'il n'y a pas lieu d'y avoir un pager, sinon un objet de type {@link XoopsPageNav} |
|
| 766 | + */ |
|
| 767 | + public function getPager() |
|
| 768 | + { |
|
| 769 | + if (!$this->isInitialized) { |
|
| 770 | + $this->filter(); |
|
| 771 | + } |
|
| 772 | + require_once XOOPS_ROOT_PATH . '/class/pagenav.php'; |
|
| 773 | + $itemsCount = $this->getCount(); |
|
| 774 | + $queryString = array(); |
|
| 775 | + if (trim($this->op) != '') { |
|
| 776 | + $queryString[$this->operationName] = $this->op; |
|
| 777 | + } |
|
| 778 | + $pagenav = null; |
|
| 779 | + |
|
| 780 | + if ($itemsCount > $this->limit) { |
|
| 781 | + foreach ($this->vars as $fieldName => $fieldProperties) { |
|
| 782 | + $formFieldName = self::PREFIX . $fieldName; // "filter_website_id" par exemple |
|
| 783 | + $queryString[$formFieldName] = $fieldProperties['data']; |
|
| 784 | + } |
|
| 785 | + // Ajout des paramètres supplémentaires éventuels |
|
| 786 | + if (count($this->additionnalPagerParameters) > 0) { |
|
| 787 | + foreach ($this->additionnalPagerParameters as $key => $value) { |
|
| 788 | + $queryString[$key] = $value; |
|
| 789 | + } |
|
| 790 | + } |
|
| 791 | + $start = $this->getStartValue(); |
|
| 792 | + $pagenav = new XoopsPageNav($itemsCount, $this->limit, $start, $this->startName, http_build_query($queryString)); |
|
| 793 | + } |
|
| 794 | + |
|
| 795 | + return $pagenav; |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + /** |
|
| 799 | + * Retourne une liste d'objets en fonction des critères définis |
|
| 800 | + * |
|
| 801 | + * @return array |
|
| 802 | + */ |
|
| 803 | + public function getObjects() |
|
| 804 | + { |
|
| 805 | + if (!$this->isInitialized) { |
|
| 806 | + $this->filter(); |
|
| 807 | + } |
|
| 808 | + $start = $this->getStartValue(); |
|
| 809 | + $limit = $this->limit; |
|
| 810 | + $criteria = $this->criteria; |
|
| 811 | + $criteria->setStart($start); |
|
| 812 | + $criteria->setLimit($limit); |
|
| 813 | + |
|
| 814 | + $criteria->setOrder($this->sortOrder); |
|
| 815 | + if (trim($this->sortField) != '') { |
|
| 816 | + $criteria->setSort($this->sortField); |
|
| 817 | + } elseif (trim($this->handler->identifierName) != '') { |
|
| 818 | + $criteria->setSort($this->handler->identifierName); |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + return $this->handler->getObjects($this->criteria); |
|
| 822 | + } |
|
| 823 | + |
|
| 824 | + /** |
|
| 825 | + * Retourne la zone html à utiliser pour créer la zone de filtre (avec sa valeur saisie si c'est le cas) |
|
| 826 | + * |
|
| 827 | + * @param string $fieldName La zone de saisie dont on veut récupérer le code html |
|
| 828 | + * @return string |
|
| 829 | + */ |
|
| 830 | + public function getFilterField($fieldName) |
|
| 831 | + { |
|
| 832 | + $html = ''; |
|
| 833 | + if (!$this->isInitialized) { |
|
| 834 | + $this->filter(); |
|
| 835 | + } |
|
| 836 | + if (!isset($this->vars[$fieldName])) { |
|
| 837 | + trigger_error('Error, unknow field'); |
|
| 838 | + |
|
| 839 | + return $html; |
|
| 840 | + } |
|
| 841 | + $fieldData = $this->vars[$fieldName]; |
|
| 842 | + $htmlFieldName = self::PREFIX . $fieldName; |
|
| 843 | + |
|
| 844 | + switch ($fieldData['fieldType']) { |
|
| 845 | + case self::FILTER_FIELD_TEXT: // Zone de texte |
|
| 846 | + $ts = MyTextSanitizer::getInstance(); |
|
| 847 | + $html = "<input type='text' name='$htmlFieldName' id='$htmlFieldName' size='" . $fieldData['size'] . "' maxlength='" . $fieldData['maxLength'] . "' value='" . $ts->htmlSpecialChars($fieldData['data']) . "' />"; |
|
| 848 | + break; |
|
| 849 | + |
|
| 850 | + case self::FILTER_FIELD_SELECT; // Select |
|
| 851 | + $style = ''; |
|
| 852 | + if (isset($fieldData['style']) && trim($fieldData['style']) != '') { |
|
| 853 | + $style = $fieldData['style']; |
|
| 854 | + } |
|
| 855 | + $html = references_utils::htmlSelect($htmlFieldName, $fieldData['values'], $fieldData['data'], $fieldData['withNull'], $style); |
|
| 856 | + break; |
|
| 857 | + |
|
| 858 | + case self::FILTER_FIELD_SELECT_YES_NO: // Select de type Oui/Non |
|
| 859 | + $html = references_utils::htmlSelect($htmlFieldName, array(2 => _YES, 1 => NO), $fieldData['data'], $fieldData['withNull']); |
|
| 860 | + break; |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + return $html; |
|
| 864 | + } |
|
| 865 | + |
|
| 866 | + /** |
|
| 867 | + * Assigne toutes les zones de filtre à un template |
|
| 868 | + * |
|
| 869 | + * @param object $xoopsTpl |
|
| 870 | + * @param boolean $asArray Est-ce qu'il faut placer le résultat dans un tableau ou assigner par nom de zone de filtre |
|
| 871 | + * @param string $arrayName Le nom du tableau à utiliser |
|
| 872 | + * @return void |
|
| 873 | + */ |
|
| 874 | + public function assignFilterFieldsToTemplate(&$xoopsTpl, $asArray = true, $arrayName = 'filterFields') |
|
| 875 | + { |
|
| 876 | + $fields = array_keys($this->vars); |
|
| 877 | + foreach ($fields as $field) { |
|
| 878 | + if (!$asArray) { |
|
| 879 | + $xoopsTpl->assign($field, $this->getFilterField($field)); |
|
| 880 | + } else { |
|
| 881 | + $xoopsTpl->append($arrayName, $this->getFilterField($field)); |
|
| 882 | + } |
|
| 883 | + } |
|
| 884 | + } |
|
| 885 | + |
|
| 886 | + /** |
|
| 887 | + * Retourne le bouton utilisé pour supprimer le filtre en cours |
|
| 888 | + * |
|
| 889 | + * @return string |
|
| 890 | + */ |
|
| 891 | + public function getClearFilterbutton() |
|
| 892 | + { |
|
| 893 | + $queryString = array(); |
|
| 894 | + $queryString[$this->operationName] = $this->op; |
|
| 895 | + if (count($this->additionnalClearButtonParameters) > 0) { |
|
| 896 | + foreach ($this->additionnalClearButtonParameters as $key => $value) { |
|
| 897 | + $queryString[$key] = $value; |
|
| 898 | + } |
|
| 899 | + } |
|
| 900 | + $queryString['cleanFilter'] = '1'; |
|
| 901 | + $baseurl = $this->baseUrl; |
|
| 902 | + |
|
| 903 | + return " <a href='$baseurl?" . http_build_query($queryString) . "' title='" . _MD_REFERENCES_CLEAN_FILTER . "'><img align='top' src='../assets/images/clear_left.png' alt='" . _MD_REFERENCES_CLEAN_FILTER . "' /></a>"; |
|
| 904 | + } |
|
| 905 | + |
|
| 906 | + /** |
|
| 907 | + * Retourne le bouton permettant de lancer le filtrage |
|
| 908 | + * |
|
| 909 | + * @param string $description Texte à faire apparaître sur le bouton |
|
| 910 | + * @param array $additionnals Champs supplémentaires à faire apparaître avec le bouton |
|
| 911 | + * @return string |
|
| 912 | + */ |
|
| 913 | + public function getGoButton($description = _GO, $additionnals = null) |
|
| 914 | + { |
|
| 915 | + $html = ''; |
|
| 916 | + if (trim($this->operationName) != '' && trim($this->op) != '') { |
|
| 917 | + $html .= "<input type='hidden' name='" . $this->operationName . "' id='" . $this->operationName . "' value='" . $this->op . "' />"; |
|
| 918 | + } |
|
| 919 | + if (!is_null($additionnals)) { |
|
| 920 | + foreach ($additionnals as $key => $value) { |
|
| 921 | + $html .= "<input type='hidden' name='" . $key . "' id='" . $key . "' value='" . $value . "' />"; |
|
| 922 | + } |
|
| 923 | + } |
|
| 924 | + $html .= "<input type='submit' name='btngo' id='btngo' value='$description' />"; |
|
| 925 | + |
|
| 926 | + return $html; |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + /** |
|
| 930 | + * Retourne le nom de la zone utilisée pour trier les données |
|
| 931 | + * |
|
| 932 | + * @return string |
|
| 933 | + */ |
|
| 934 | + public function getSortField() |
|
| 935 | + { |
|
| 936 | + return $this->sortField; |
|
| 937 | + } |
|
| 938 | + |
|
| 939 | + /** |
|
| 940 | + * Retourne le sens de tri utilisé pour trier les données |
|
| 941 | + * |
|
| 942 | + * @return string |
|
| 943 | + */ |
|
| 944 | + public function getSortOrder() |
|
| 945 | + { |
|
| 946 | + return $this->sortOrder(); |
|
| 947 | + } |
|
| 948 | + |
|
| 949 | + /** |
|
| 950 | + * Retourne la valeur d'un champ |
|
| 951 | + * |
|
| 952 | + * @param string $fieldName |
|
| 953 | + * @return mixed La valeur du champ ou null si on ne trouve pas la zone |
|
| 954 | + */ |
|
| 955 | + public function getFieldValue($fieldName) |
|
| 956 | + { |
|
| 957 | + $ret = null; |
|
| 958 | + if (isset($this->vars[$fieldName])) { |
|
| 959 | + $ret = $this->vars[$fieldName]['data']; |
|
| 960 | + } |
|
| 961 | + |
|
| 962 | + return $ret; |
|
| 963 | + } |
|
| 964 | 964 | } |
@@ -33,654 +33,654 @@ discard block |
||
| 33 | 33 | use WideImage\WideImage; |
| 34 | 34 | |
| 35 | 35 | if (!defined('XOOPS_ROOT_PATH')) { |
| 36 | - die('XOOPS root path not defined'); |
|
| 36 | + die('XOOPS root path not defined'); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | class references_utils |
| 40 | 40 | { |
| 41 | - // Pour la portabilité de module à module |
|
| 42 | - const MODULE_NAME = 'references'; |
|
| 43 | - const MODULE_DIRNAME = REFERENCES_DIRNAME; |
|
| 44 | - const MODULE_PATH = REFERENCES_PATH; |
|
| 45 | - const MODULE_URL = REFERENCES_URL; |
|
| 46 | - const MODULE_JS_URL = REFERENCES_JS_URL; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Access the only instance of this class |
|
| 50 | - * |
|
| 51 | - * @return object |
|
| 52 | - * |
|
| 53 | - * @static |
|
| 54 | - * @staticvar object |
|
| 55 | - */ |
|
| 56 | - public static function &getInstance() |
|
| 57 | - { |
|
| 58 | - static $instance; |
|
| 59 | - if (!isset($instance)) { |
|
| 60 | - $instance = new references_utils(); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - return $instance; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Returns a module's option (with cache) |
|
| 68 | - * |
|
| 69 | - * @param string $option module option's name |
|
| 70 | - * @param boolean $withCache Do we have to use some cache ? |
|
| 71 | - * @return mixed option's value |
|
| 72 | - */ |
|
| 73 | - public static function getModuleOption($option, $withCache = true) |
|
| 74 | - { |
|
| 75 | - global $xoopsModuleConfig, $xoopsModule; |
|
| 76 | - $repmodule = self::MODULE_NAME; |
|
| 77 | - static $options = array(); |
|
| 78 | - if (is_array($options) && array_key_exists($option, $options) && $withCache) { |
|
| 79 | - return $options[$option]; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - $retval = false; |
|
| 83 | - if (isset($xoopsModuleConfig) && (is_object($xoopsModule) && $xoopsModule->getVar('dirname') == $repmodule && $xoopsModule->getVar('isactive'))) { |
|
| 84 | - if (isset($xoopsModuleConfig[$option])) { |
|
| 85 | - $retval = $xoopsModuleConfig[$option]; |
|
| 86 | - } |
|
| 87 | - } else { |
|
| 88 | - $module_handler = xoops_getHandler('module'); |
|
| 89 | - $module = $module_handler->getByDirname($repmodule); |
|
| 90 | - $config_handler = xoops_getHandler('config'); |
|
| 91 | - if ($module) { |
|
| 92 | - $moduleConfig = $config_handler->getConfigsByCat(0, $module->getVar('mid')); |
|
| 93 | - if (isset($moduleConfig[$option])) { |
|
| 94 | - $retval = $moduleConfig[$option]; |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - $options[$option] = $retval; |
|
| 99 | - |
|
| 100 | - return $retval; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Is Xoops 2.3.x ? |
|
| 105 | - * |
|
| 106 | - * @return boolean need to say it ? |
|
| 107 | - */ |
|
| 108 | - public static function isX23() |
|
| 109 | - { |
|
| 110 | - $x23 = false; |
|
| 111 | - $xv = str_replace('XOOPS ', '', XOOPS_VERSION); |
|
| 112 | - if ((int)substr($xv, 2, 1) >= 3) { |
|
| 113 | - $x23 = true; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - return $x23; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Retreive an editor according to the module's option "form_options" |
|
| 121 | - * |
|
| 122 | - * @param string $caption Caption to give to the editor |
|
| 123 | - * @param string $name Editor's name |
|
| 124 | - * @param string $value Editor's value |
|
| 125 | - * @param string $width Editor's width |
|
| 126 | - * @param string $height Editor's height |
|
| 127 | - * @param string $supplemental |
|
| 128 | - * @return object The editor to use |
|
| 129 | - */ |
|
| 130 | - public static function getWysiwygForm($caption, $name, $value = '', $width = '100%', $height = '400px', $supplemental = '') |
|
| 131 | - { |
|
| 132 | - $editor = false; |
|
| 133 | - $editor_configs = array(); |
|
| 134 | - $editor_configs['name'] = $name; |
|
| 135 | - $editor_configs['value'] = $value; |
|
| 136 | - $editor_configs['rows'] = 35; |
|
| 137 | - $editor_configs['cols'] = 60; |
|
| 138 | - $editor_configs['width'] = '100%'; |
|
| 139 | - $editor_configs['height'] = '400px'; |
|
| 140 | - |
|
| 141 | - $editor_option = strtolower(self::getModuleOption('form_options')); |
|
| 142 | - |
|
| 143 | - if (self::isX23()) { |
|
| 144 | - $editor = new XoopsFormEditor($caption, $editor_option, $editor_configs); |
|
| 145 | - |
|
| 146 | - return $editor; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - // Only for Xoops 2.0.x |
|
| 150 | - switch ($editor_option) { |
|
| 151 | - case 'fckeditor': |
|
| 152 | - if (is_readable(XOOPS_ROOT_PATH . '/class/fckeditor/formfckeditor.php')) { |
|
| 153 | - require_once(XOOPS_ROOT_PATH . '/class/fckeditor/formfckeditor.php'); |
|
| 154 | - $editor = new XoopsFormFckeditor($caption, $name, $value); |
|
| 155 | - } |
|
| 156 | - break; |
|
| 157 | - |
|
| 158 | - case 'htmlarea': |
|
| 159 | - if (is_readable(XOOPS_ROOT_PATH . '/class/htmlarea/formhtmlarea.php')) { |
|
| 160 | - require_once(XOOPS_ROOT_PATH . '/class/htmlarea/formhtmlarea.php'); |
|
| 161 | - $editor = new XoopsFormHtmlarea($caption, $name, $value); |
|
| 162 | - } |
|
| 163 | - break; |
|
| 164 | - |
|
| 165 | - case 'dhtmltextarea': |
|
| 166 | - $editor = new XoopsFormDhtmlTextArea($caption, $name, $value, 10, 50, $supplemental); |
|
| 167 | - break; |
|
| 168 | - |
|
| 169 | - case 'textarea': |
|
| 170 | - $editor = new XoopsFormTextArea($caption, $name, $value); |
|
| 171 | - break; |
|
| 172 | - |
|
| 173 | - case 'tinyeditor': |
|
| 174 | - case 'tinymce': |
|
| 175 | - if (is_readable(XOOPS_ROOT_PATH . '/class/xoopseditor/tinyeditor/formtinyeditortextarea.php')) { |
|
| 176 | - require_once XOOPS_ROOT_PATH . '/class/xoopseditor/tinyeditor/formtinyeditortextarea.php'; |
|
| 177 | - $editor = new XoopsFormTinyeditorTextArea(array('caption' => $caption, 'name' => $name, 'value' => $value, 'width' => '100%', 'height' => '400px')); |
|
| 178 | - } |
|
| 179 | - break; |
|
| 180 | - |
|
| 181 | - case 'koivi': |
|
| 182 | - if (is_readable(XOOPS_ROOT_PATH . '/class/wysiwyg/formwysiwygtextarea.php')) { |
|
| 183 | - require_once(XOOPS_ROOT_PATH . '/class/wysiwyg/formwysiwygtextarea.php'); |
|
| 184 | - $editor = new XoopsFormWysiwygTextArea($caption, $name, $value, $width, $height, ''); |
|
| 185 | - } |
|
| 186 | - break; |
|
| 187 | - } |
|
| 188 | - if (!is_object($editor)) { |
|
| 189 | - trigger_error('Error, impossible to get the requested text editor', E_USER_ERROR); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - return $editor; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Create (in a link) a javascript confirmation's box |
|
| 197 | - * |
|
| 198 | - * @param string $message Message to display |
|
| 199 | - * @param boolean $form Is this a confirmation for a form ? |
|
| 200 | - * @return string the javascript code to insert in the link (or in the form) |
|
| 201 | - */ |
|
| 202 | - public static function javascriptLinkConfirm($message, $form = false) |
|
| 203 | - { |
|
| 204 | - if (!$form) { |
|
| 205 | - return "onclick=\"javascript:return confirm('" . str_replace("'", ' ', $message) . "')\""; |
|
| 206 | - } else { |
|
| 207 | - return "onSubmit=\"javascript:return confirm('" . str_replace("'", ' ', $message) . "')\""; |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Get current user IP |
|
| 213 | - * |
|
| 214 | - * @return string IP address (format Ipv4) |
|
| 215 | - */ |
|
| 216 | - public static function IP() |
|
| 217 | - { |
|
| 218 | - $proxy_ip = ''; |
|
| 219 | - if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
|
| 220 | - $proxy_ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
|
| 221 | - } elseif (!empty($_SERVER['HTTP_X_FORWARDED'])) { |
|
| 222 | - $proxy_ip = $_SERVER['HTTP_X_FORWARDED']; |
|
| 223 | - } elseif (!empty($_SERVER['HTTP_FORWARDED_FOR'])) { |
|
| 224 | - $proxy_ip = $_SERVER['HTTP_FORWARDED_FOR']; |
|
| 225 | - } elseif (!empty($_SERVER['HTTP_FORWARDED'])) { |
|
| 226 | - $proxy_ip = $_SERVER['HTTP_FORWARDED']; |
|
| 227 | - } elseif (!empty($_SERVER['HTTP_VIA'])) { |
|
| 228 | - $proxy_ip = $_SERVER['HTTP_VIA']; |
|
| 229 | - } elseif (!empty($_SERVER['HTTP_X_COMING_FROM'])) { |
|
| 230 | - $proxy_ip = $_SERVER['HTTP_X_COMING_FROM']; |
|
| 231 | - } elseif (!empty($_SERVER['HTTP_COMING_FROM'])) { |
|
| 232 | - $proxy_ip = $_SERVER['HTTP_COMING_FROM']; |
|
| 233 | - } |
|
| 234 | - $regs = array(); |
|
| 235 | - if (!empty($proxy_ip) && $is_ip = ereg('^([0-9]{1,3}\.){3,3}[0-9]{1,3}', $proxy_ip, $regs) && count($regs) > 0) { |
|
| 236 | - $the_IP = $regs[0]; |
|
| 237 | - } else { |
|
| 238 | - $the_IP = $_SERVER['REMOTE_ADDR']; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - return $the_IP; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Set the page's title, meta description and meta keywords |
|
| 246 | - * Datas are supposed to be sanitized |
|
| 247 | - * |
|
| 248 | - * @param string $pageTitle Page's Title |
|
| 249 | - * @param string $metaDescription Page's meta description |
|
| 250 | - * @param string $metaKeywords Page's meta keywords |
|
| 251 | - * @return void |
|
| 252 | - */ |
|
| 253 | - public static function setMetas($pageTitle = '', $metaDescription = '', $metaKeywords = '') |
|
| 254 | - { |
|
| 255 | - global $xoTheme, $xoTheme, $xoopsTpl; |
|
| 256 | - $xoopsTpl->assign('xoops_pagetitle', $pageTitle); |
|
| 257 | - if (isset($xoTheme) && is_object($xoTheme)) { |
|
| 258 | - if (!empty($metaKeywords)) { |
|
| 259 | - $xoTheme->addMeta('meta', 'keywords', $metaKeywords); |
|
| 260 | - } |
|
| 261 | - if (!empty($metaDescription)) { |
|
| 262 | - $xoTheme->addMeta('meta', 'description', $metaDescription); |
|
| 263 | - } |
|
| 264 | - } elseif (isset($xoopsTpl) && is_object($xoopsTpl)) { // Compatibility for old Xoops versions |
|
| 265 | - if (!empty($metaKeywords)) { |
|
| 266 | - $xoopsTpl->assign('xoops_meta_keywords', $metaKeywords); |
|
| 267 | - } |
|
| 268 | - if (!empty($metaDescription)) { |
|
| 269 | - $xoopsTpl->assign('xoops_meta_description', $metaDescription); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Send an email from a template to a list of recipients |
|
| 276 | - * |
|
| 277 | - * @param $tplName |
|
| 278 | - * @param array $recipients List of recipients |
|
| 279 | - * @param string $subject Email's subject |
|
| 280 | - * @param array $variables Varirables to give to the template |
|
| 281 | - * @return bool Result of the send |
|
| 282 | - * @internal param string $tpl_name Template's name |
|
| 283 | - */ |
|
| 284 | - public static function sendEmailFromTpl($tplName, $recipients, $subject, $variables) |
|
| 285 | - { |
|
| 286 | - global $xoopsConfig; |
|
| 287 | - require_once XOOPS_ROOT_PATH . '/class/xoopsmailer.php'; |
|
| 288 | - if (!is_array($recipients)) { |
|
| 289 | - if (trim($recipients) == '') { |
|
| 290 | - return false; |
|
| 291 | - } |
|
| 292 | - } else { |
|
| 293 | - if (count($recipients) == 0) { |
|
| 294 | - return false; |
|
| 295 | - } |
|
| 296 | - } |
|
| 297 | - if (function_exists('xoops_getMailer')) { |
|
| 298 | - $xoopsMailer =& xoops_getMailer(); |
|
| 299 | - } else { |
|
| 300 | - $xoopsMailer =& getMailer(); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - $xoopsMailer->useMail(); |
|
| 304 | - $xoopsMailer->setTemplateDir(XOOPS_ROOT_PATH . '/modules/' . self::MODULE_NAME . '/language/' . $xoopsConfig['language'] . '/mail_template'); |
|
| 305 | - $xoopsMailer->setTemplate($tplName); |
|
| 306 | - $xoopsMailer->setToEmails($recipients); |
|
| 307 | - // TODO: Change ! |
|
| 308 | - // $xoopsMailer->setFromEmail('[email protected]'); |
|
| 309 | - //$xoopsMailer->setFromName('MonSite'); |
|
| 310 | - $xoopsMailer->setSubject($subject); |
|
| 311 | - foreach ($variables as $key => $value) { |
|
| 312 | - $xoopsMailer->assign($key, $value); |
|
| 313 | - } |
|
| 314 | - $res = $xoopsMailer->send(); |
|
| 315 | - unset($xoopsMailer); |
|
| 316 | - $filename = XOOPS_UPLOAD_PATH . '/logmail_' . self::MODULE_NAME . '.php'; |
|
| 317 | - if (!file_exists($filename)) { |
|
| 318 | - $fp = @fopen($filename, 'a'); |
|
| 319 | - if ($fp) { |
|
| 320 | - fwrite($fp, '<?php exit(); ?>'); |
|
| 321 | - fclose($fp); |
|
| 322 | - } |
|
| 323 | - } |
|
| 324 | - $fp = @fopen($filename, 'a'); |
|
| 325 | - |
|
| 326 | - if ($fp) { |
|
| 327 | - fwrite($fp, str_repeat('-', 120) . "\n"); |
|
| 328 | - fwrite($fp, date('d/m/Y H:i:s') . "\n"); |
|
| 329 | - fwrite($fp, 'Template name : ' . $tplName . "\n"); |
|
| 330 | - fwrite($fp, 'Email subject : ' . $subject . "\n"); |
|
| 331 | - if (is_array($recipients)) { |
|
| 332 | - fwrite($fp, 'Recipient(s) : ' . implode(',', $recipients) . "\n"); |
|
| 333 | - } else { |
|
| 334 | - fwrite($fp, 'Recipient(s) : ' . $recipients . "\n"); |
|
| 335 | - } |
|
| 336 | - fwrite($fp, 'Transmited variables : ' . implode(',', $variables) . "\n"); |
|
| 337 | - fclose($fp); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - return $res; |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Remove module's cache |
|
| 345 | - */ |
|
| 346 | - public static function updateCache() |
|
| 347 | - { |
|
| 348 | - global $xoopsModule; |
|
| 349 | - $folder = $xoopsModule->getVar('dirname'); |
|
| 350 | - $tpllist = array(); |
|
| 351 | - require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php'; |
|
| 352 | - require_once XOOPS_ROOT_PATH . '/class/template.php'; |
|
| 353 | - $tplfile_handler = xoops_getHandler('tplfile'); |
|
| 354 | - $tpllist = $tplfile_handler->find(null, null, null, $folder); |
|
| 355 | - xoops_template_clear_module_cache($xoopsModule->getVar('mid')); // Clear module's blocks cache |
|
| 356 | - |
|
| 357 | - foreach ($tpllist as $onetemplate) { // Remove cache for each page. |
|
| 358 | - if ($onetemplate->getVar('tpl_type') === 'module') { |
|
| 359 | - // Note, I've been testing all the other methods (like the one of Smarty) and none of them run, that's why I have used this code |
|
| 360 | - $files_del = array(); |
|
| 361 | - $files_del = glob(XOOPS_CACHE_PATH . '/*' . $onetemplate->getVar('tpl_file') . '*'); |
|
| 362 | - if (count($files_del) > 0 && is_array($files_del)) { |
|
| 363 | - foreach ($files_del as $one_file) { |
|
| 364 | - if (is_file($one_file)) { |
|
| 365 | - unlink($one_file); |
|
| 366 | - } |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Redirect user with a message |
|
| 375 | - * |
|
| 376 | - * @param string $message message to display |
|
| 377 | - * @param string $url The place where to go |
|
| 378 | - * @param integer timeout Time to wait before to redirect |
|
| 379 | - */ |
|
| 380 | - public static function redirect($message = '', $url = 'index.php', $time = 2) |
|
| 381 | - { |
|
| 382 | - redirect_header($url, $time, $message); |
|
| 383 | - exit(); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Internal function used to get the handler of the current module |
|
| 388 | - * |
|
| 389 | - * @return object The module |
|
| 390 | - */ |
|
| 391 | - public static function getModule() |
|
| 392 | - { |
|
| 393 | - static $mymodule; |
|
| 394 | - if (!isset($mymodule)) { |
|
| 395 | - global $xoopsModule; |
|
| 396 | - if (isset($xoopsModule) && is_object($xoopsModule) && $xoopsModule->getVar('dirname') == REFERENCES_DIRNAME) { |
|
| 397 | - $mymodule =& $xoopsModule; |
|
| 398 | - } else { |
|
| 399 | - $hModule = xoops_getHandler('module'); |
|
| 400 | - $mymodule = $hModule->getByDirname(REFERENCES_DIRNAME); |
|
| 401 | - } |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - return $mymodule; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Returns the module's name (as defined by the user in the module manager) with cache |
|
| 409 | - * @return string Module's name |
|
| 410 | - */ |
|
| 411 | - public static function getModuleName() |
|
| 412 | - { |
|
| 413 | - static $moduleName; |
|
| 414 | - if (!isset($moduleName)) { |
|
| 415 | - $mymodule = self::getModule(); |
|
| 416 | - $moduleName = $mymodule->getVar('name'); |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - return $moduleName; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Create a title for the href tags inside html links |
|
| 424 | - * |
|
| 425 | - * @param string $title Text to use |
|
| 426 | - * @return string Formated text |
|
| 427 | - */ |
|
| 428 | - public static function makeHrefTitle($title) |
|
| 429 | - { |
|
| 430 | - $s = "\"'"; |
|
| 431 | - $r = ' '; |
|
| 432 | - |
|
| 433 | - return strtr($title, $s, $r); |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * Retourne la liste des utilisateurs appartenants � un groupe |
|
| 438 | - * |
|
| 439 | - * @param int $groupId Searched group |
|
| 440 | - * @return array Array of XoopsUsers |
|
| 441 | - */ |
|
| 442 | - public static function getUsersFromGroup($groupId) |
|
| 443 | - { |
|
| 444 | - $users = array(); |
|
| 445 | - $member_handler = xoops_getHandler('member'); |
|
| 446 | - $users = $member_handler->getUsersByGroup($groupId, true); |
|
| 447 | - |
|
| 448 | - return $users; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Retourne les ID des utilisateurs faisant partie de plusieurs groupes |
|
| 453 | - * @param array $groups Les ID des groupes dont on recherche les ID utilisateurs |
|
| 454 | - * @return array Les ID utilisateurs |
|
| 455 | - */ |
|
| 456 | - public static function getUsersIdsFromGroups($groups) |
|
| 457 | - { |
|
| 458 | - $usersIds = array(); |
|
| 459 | - $member_handler = xoops_getHandler('member'); |
|
| 460 | - foreach ($groups as $groupId) { |
|
| 461 | - $temporaryGroup = array(); |
|
| 462 | - $temporaryGroup = $member_handler->getUsersByGroup($groupId); |
|
| 463 | - if (count($temporaryGroup) > 0) { |
|
| 464 | - $usersIds = array_merge($usersIds, $temporaryGroup); |
|
| 465 | - } |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - return array_unique($usersIds); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - /** |
|
| 472 | - * Retourne la liste des emails des utilisateurs membres d'un groupe |
|
| 473 | - * |
|
| 474 | - * @param $groupId |
|
| 475 | - * @return array Emails list |
|
| 476 | - * @internal param int $group_id Group's number |
|
| 477 | - */ |
|
| 478 | - public static function getEmailsFromGroup($groupId) |
|
| 479 | - { |
|
| 480 | - $ret = array(); |
|
| 481 | - $users = self::getUsersFromGroup($groupId); |
|
| 482 | - foreach ($users as $user) { |
|
| 483 | - $ret[] = $user->getVar('email'); |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - return $ret; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * V�rifie que l'utilisateur courant fait partie du groupe des administrateurs |
|
| 491 | - * |
|
| 492 | - * @return booleean Admin or not |
|
| 493 | - */ |
|
| 494 | - public static function isAdmin() |
|
| 495 | - { |
|
| 496 | - global $xoopsUser, $xoopsModule; |
|
| 497 | - if (is_object($xoopsUser)) { |
|
| 498 | - if (in_array(XOOPS_GROUP_ADMIN, $xoopsUser->getGroups())) { |
|
| 499 | - return true; |
|
| 500 | - } elseif (isset($xoopsModule) && $xoopsUser->isAdmin($xoopsModule->getVar('mid'))) { |
|
| 501 | - return true; |
|
| 502 | - } |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - return false; |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - /** |
|
| 509 | - * Returns the current date in the Mysql format |
|
| 510 | - * |
|
| 511 | - * @return string Date in the Mysql format |
|
| 512 | - */ |
|
| 513 | - public static function getCurrentSQLDate() |
|
| 514 | - { |
|
| 515 | - return date('Y-m-d'); // 2007-05-02 |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - public static function getCurrentSQLDateTime() |
|
| 519 | - { |
|
| 520 | - return date('Y-m-d H:i:s'); // 2007-05-02 |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Convert a Mysql date to the human's format |
|
| 525 | - * |
|
| 526 | - * @param string $date The date to convert |
|
| 527 | - * @param string $format |
|
| 528 | - * @return string The date in a human form |
|
| 529 | - */ |
|
| 530 | - public static function SQLDateToHuman($date, $format = 'l') |
|
| 531 | - { |
|
| 532 | - if ($date != '0000-00-00' && xoops_trim($date) != '') { |
|
| 533 | - return formatTimestamp(strtotime($date), $format); |
|
| 534 | - } else { |
|
| 535 | - return ''; |
|
| 536 | - } |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - /** |
|
| 540 | - * Convert a timestamp to a Mysql date |
|
| 541 | - * |
|
| 542 | - * @param integer $timestamp The timestamp to use |
|
| 543 | - * @return string The date in the Mysql format |
|
| 544 | - */ |
|
| 545 | - public static function timestampToMysqlDate($timestamp) |
|
| 546 | - { |
|
| 547 | - return date('Y-m-d', (int)$timestamp); |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * Conversion d'un dateTime Mysql en date lisible en fran�ais |
|
| 552 | - * @param $dateTime |
|
| 553 | - * @return bool|string |
|
| 554 | - */ |
|
| 555 | - public static function sqlDateTimeToFrench($dateTime) |
|
| 556 | - { |
|
| 557 | - return date('d/m/Y H:i:s', strtotime($dateTime)); |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * Convert a timestamp to a Mysql datetime form |
|
| 562 | - * @param integer $timestamp The timestamp to use |
|
| 563 | - * @return string The date and time in the Mysql format |
|
| 564 | - */ |
|
| 565 | - public static function timestampToMysqlDateTime($timestamp) |
|
| 566 | - { |
|
| 567 | - return date('Y-m-d H:i:s', $timestamp); |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * This function indicates if the current Xoops version needs to add asterisks to required fields in forms |
|
| 572 | - * |
|
| 573 | - * @return boolean Yes = we need to add them, false = no |
|
| 574 | - */ |
|
| 575 | - public static function needsAsterisk() |
|
| 576 | - { |
|
| 577 | - if (self::isX23()) { |
|
| 578 | - return false; |
|
| 579 | - } |
|
| 580 | - if (strpos(strtolower(XOOPS_VERSION), 'impresscms') !== false) { |
|
| 581 | - return false; |
|
| 582 | - } |
|
| 583 | - if (strpos(strtolower(XOOPS_VERSION), 'legacy') === false) { |
|
| 584 | - $xv = xoops_trim(str_replace('XOOPS ', '', XOOPS_VERSION)); |
|
| 585 | - if ((int)substr($xv, 4, 2) >= 17) { |
|
| 586 | - return false; |
|
| 587 | - } |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - return true; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * Mark the mandatory fields of a form with a star |
|
| 595 | - * |
|
| 596 | - * @param object $sform The form to modify |
|
| 597 | - * @return object The modified form |
|
| 598 | - * @internal param string $caracter The character to use to mark fields |
|
| 599 | - */ |
|
| 600 | - public static function &formMarkRequiredFields(&$sform) |
|
| 601 | - { |
|
| 602 | - if (self::needsAsterisk()) { |
|
| 603 | - $required = array(); |
|
| 604 | - foreach ($sform->getRequired() as $item) { |
|
| 605 | - $required[] = $item->_name; |
|
| 606 | - } |
|
| 607 | - $elements = array(); |
|
| 608 | - $elements = &$sform->getElements(); |
|
| 609 | - $cnt = count($elements); |
|
| 610 | - for ($i = 0; $i < $cnt; ++$i) { |
|
| 611 | - if (is_object($elements[$i]) && in_array($elements[$i]->_name, $required)) { |
|
| 612 | - $elements[$i]->_caption .= ' *'; |
|
| 613 | - } |
|
| 614 | - } |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - return $sform; |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * Create an html heading (from h1 to h6) |
|
| 622 | - * |
|
| 623 | - * @param string $title The text to use |
|
| 624 | - * @param integer $level Level to return |
|
| 625 | - * @return string The heading |
|
| 626 | - */ |
|
| 627 | - public static function htitle($title = '', $level = 1) |
|
| 628 | - { |
|
| 629 | - printf('<h%01d>%s</h%01d>', $level, $title, $level); |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * Create a unique upload filename |
|
| 634 | - * |
|
| 635 | - * @param string $folder The folder where the file will be saved |
|
| 636 | - * @param string $fileName Original filename (coming from the user) |
|
| 637 | - * @param boolean $trimName Do we need to create a short unique name ? |
|
| 638 | - * @return string The unique filename to use (with its extension) |
|
| 639 | - */ |
|
| 640 | - public static function createUploadName($folder, $fileName, $trimName = false) |
|
| 641 | - { |
|
| 642 | - $workingfolder = $folder; |
|
| 643 | - if (substr($workingfolder, strlen($workingfolder) - 1, 1) !== '/') { |
|
| 644 | - $workingfolder .= '/'; |
|
| 645 | - } |
|
| 646 | - $ext = basename($fileName); |
|
| 647 | - $ext = explode('.', $ext); |
|
| 648 | - $ext = '.' . $ext[count($ext) - 1]; |
|
| 649 | - $true = true; |
|
| 650 | - while ($true) { |
|
| 651 | - $ipbits = explode('.', $_SERVER['REMOTE_ADDR']); |
|
| 652 | - list($usec, $sec) = explode(' ', microtime()); |
|
| 653 | - $usec = (integer)($usec * 65536); |
|
| 654 | - $sec = ((integer)$sec) & 0xFFFF; |
|
| 655 | - |
|
| 656 | - if ($trimName) { |
|
| 657 | - $uid = sprintf('%06x%04x%04x', ($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec); |
|
| 658 | - } else { |
|
| 659 | - $uid = sprintf('%08x-%04x-%04x', ($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec); |
|
| 660 | - } |
|
| 661 | - if (!file_exists($workingfolder . $uid . $ext)) { |
|
| 662 | - $true = false; |
|
| 663 | - } |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - return $uid . $ext; |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - /** |
|
| 670 | - * Replace html entities with their ASCII equivalent |
|
| 671 | - * |
|
| 672 | - * @param string $chaine The string undecode |
|
| 673 | - * @return string The undecoded string |
|
| 674 | - */ |
|
| 675 | - public static function unhtml($chaine) |
|
| 676 | - { |
|
| 677 | - $search = $replace = array(); |
|
| 678 | - $chaine = html_entity_decode($chaine); |
|
| 679 | - |
|
| 680 | - for ($i = 0; $i <= 255; ++$i) { |
|
| 681 | - $search[] = '&#' . $i . ';'; |
|
| 682 | - $replace[] = chr($i); |
|
| 683 | - } |
|
| 41 | + // Pour la portabilité de module à module |
|
| 42 | + const MODULE_NAME = 'references'; |
|
| 43 | + const MODULE_DIRNAME = REFERENCES_DIRNAME; |
|
| 44 | + const MODULE_PATH = REFERENCES_PATH; |
|
| 45 | + const MODULE_URL = REFERENCES_URL; |
|
| 46 | + const MODULE_JS_URL = REFERENCES_JS_URL; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Access the only instance of this class |
|
| 50 | + * |
|
| 51 | + * @return object |
|
| 52 | + * |
|
| 53 | + * @static |
|
| 54 | + * @staticvar object |
|
| 55 | + */ |
|
| 56 | + public static function &getInstance() |
|
| 57 | + { |
|
| 58 | + static $instance; |
|
| 59 | + if (!isset($instance)) { |
|
| 60 | + $instance = new references_utils(); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + return $instance; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Returns a module's option (with cache) |
|
| 68 | + * |
|
| 69 | + * @param string $option module option's name |
|
| 70 | + * @param boolean $withCache Do we have to use some cache ? |
|
| 71 | + * @return mixed option's value |
|
| 72 | + */ |
|
| 73 | + public static function getModuleOption($option, $withCache = true) |
|
| 74 | + { |
|
| 75 | + global $xoopsModuleConfig, $xoopsModule; |
|
| 76 | + $repmodule = self::MODULE_NAME; |
|
| 77 | + static $options = array(); |
|
| 78 | + if (is_array($options) && array_key_exists($option, $options) && $withCache) { |
|
| 79 | + return $options[$option]; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + $retval = false; |
|
| 83 | + if (isset($xoopsModuleConfig) && (is_object($xoopsModule) && $xoopsModule->getVar('dirname') == $repmodule && $xoopsModule->getVar('isactive'))) { |
|
| 84 | + if (isset($xoopsModuleConfig[$option])) { |
|
| 85 | + $retval = $xoopsModuleConfig[$option]; |
|
| 86 | + } |
|
| 87 | + } else { |
|
| 88 | + $module_handler = xoops_getHandler('module'); |
|
| 89 | + $module = $module_handler->getByDirname($repmodule); |
|
| 90 | + $config_handler = xoops_getHandler('config'); |
|
| 91 | + if ($module) { |
|
| 92 | + $moduleConfig = $config_handler->getConfigsByCat(0, $module->getVar('mid')); |
|
| 93 | + if (isset($moduleConfig[$option])) { |
|
| 94 | + $retval = $moduleConfig[$option]; |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + $options[$option] = $retval; |
|
| 99 | + |
|
| 100 | + return $retval; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Is Xoops 2.3.x ? |
|
| 105 | + * |
|
| 106 | + * @return boolean need to say it ? |
|
| 107 | + */ |
|
| 108 | + public static function isX23() |
|
| 109 | + { |
|
| 110 | + $x23 = false; |
|
| 111 | + $xv = str_replace('XOOPS ', '', XOOPS_VERSION); |
|
| 112 | + if ((int)substr($xv, 2, 1) >= 3) { |
|
| 113 | + $x23 = true; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + return $x23; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Retreive an editor according to the module's option "form_options" |
|
| 121 | + * |
|
| 122 | + * @param string $caption Caption to give to the editor |
|
| 123 | + * @param string $name Editor's name |
|
| 124 | + * @param string $value Editor's value |
|
| 125 | + * @param string $width Editor's width |
|
| 126 | + * @param string $height Editor's height |
|
| 127 | + * @param string $supplemental |
|
| 128 | + * @return object The editor to use |
|
| 129 | + */ |
|
| 130 | + public static function getWysiwygForm($caption, $name, $value = '', $width = '100%', $height = '400px', $supplemental = '') |
|
| 131 | + { |
|
| 132 | + $editor = false; |
|
| 133 | + $editor_configs = array(); |
|
| 134 | + $editor_configs['name'] = $name; |
|
| 135 | + $editor_configs['value'] = $value; |
|
| 136 | + $editor_configs['rows'] = 35; |
|
| 137 | + $editor_configs['cols'] = 60; |
|
| 138 | + $editor_configs['width'] = '100%'; |
|
| 139 | + $editor_configs['height'] = '400px'; |
|
| 140 | + |
|
| 141 | + $editor_option = strtolower(self::getModuleOption('form_options')); |
|
| 142 | + |
|
| 143 | + if (self::isX23()) { |
|
| 144 | + $editor = new XoopsFormEditor($caption, $editor_option, $editor_configs); |
|
| 145 | + |
|
| 146 | + return $editor; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + // Only for Xoops 2.0.x |
|
| 150 | + switch ($editor_option) { |
|
| 151 | + case 'fckeditor': |
|
| 152 | + if (is_readable(XOOPS_ROOT_PATH . '/class/fckeditor/formfckeditor.php')) { |
|
| 153 | + require_once(XOOPS_ROOT_PATH . '/class/fckeditor/formfckeditor.php'); |
|
| 154 | + $editor = new XoopsFormFckeditor($caption, $name, $value); |
|
| 155 | + } |
|
| 156 | + break; |
|
| 157 | + |
|
| 158 | + case 'htmlarea': |
|
| 159 | + if (is_readable(XOOPS_ROOT_PATH . '/class/htmlarea/formhtmlarea.php')) { |
|
| 160 | + require_once(XOOPS_ROOT_PATH . '/class/htmlarea/formhtmlarea.php'); |
|
| 161 | + $editor = new XoopsFormHtmlarea($caption, $name, $value); |
|
| 162 | + } |
|
| 163 | + break; |
|
| 164 | + |
|
| 165 | + case 'dhtmltextarea': |
|
| 166 | + $editor = new XoopsFormDhtmlTextArea($caption, $name, $value, 10, 50, $supplemental); |
|
| 167 | + break; |
|
| 168 | + |
|
| 169 | + case 'textarea': |
|
| 170 | + $editor = new XoopsFormTextArea($caption, $name, $value); |
|
| 171 | + break; |
|
| 172 | + |
|
| 173 | + case 'tinyeditor': |
|
| 174 | + case 'tinymce': |
|
| 175 | + if (is_readable(XOOPS_ROOT_PATH . '/class/xoopseditor/tinyeditor/formtinyeditortextarea.php')) { |
|
| 176 | + require_once XOOPS_ROOT_PATH . '/class/xoopseditor/tinyeditor/formtinyeditortextarea.php'; |
|
| 177 | + $editor = new XoopsFormTinyeditorTextArea(array('caption' => $caption, 'name' => $name, 'value' => $value, 'width' => '100%', 'height' => '400px')); |
|
| 178 | + } |
|
| 179 | + break; |
|
| 180 | + |
|
| 181 | + case 'koivi': |
|
| 182 | + if (is_readable(XOOPS_ROOT_PATH . '/class/wysiwyg/formwysiwygtextarea.php')) { |
|
| 183 | + require_once(XOOPS_ROOT_PATH . '/class/wysiwyg/formwysiwygtextarea.php'); |
|
| 184 | + $editor = new XoopsFormWysiwygTextArea($caption, $name, $value, $width, $height, ''); |
|
| 185 | + } |
|
| 186 | + break; |
|
| 187 | + } |
|
| 188 | + if (!is_object($editor)) { |
|
| 189 | + trigger_error('Error, impossible to get the requested text editor', E_USER_ERROR); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + return $editor; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Create (in a link) a javascript confirmation's box |
|
| 197 | + * |
|
| 198 | + * @param string $message Message to display |
|
| 199 | + * @param boolean $form Is this a confirmation for a form ? |
|
| 200 | + * @return string the javascript code to insert in the link (or in the form) |
|
| 201 | + */ |
|
| 202 | + public static function javascriptLinkConfirm($message, $form = false) |
|
| 203 | + { |
|
| 204 | + if (!$form) { |
|
| 205 | + return "onclick=\"javascript:return confirm('" . str_replace("'", ' ', $message) . "')\""; |
|
| 206 | + } else { |
|
| 207 | + return "onSubmit=\"javascript:return confirm('" . str_replace("'", ' ', $message) . "')\""; |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Get current user IP |
|
| 213 | + * |
|
| 214 | + * @return string IP address (format Ipv4) |
|
| 215 | + */ |
|
| 216 | + public static function IP() |
|
| 217 | + { |
|
| 218 | + $proxy_ip = ''; |
|
| 219 | + if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
|
| 220 | + $proxy_ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
|
| 221 | + } elseif (!empty($_SERVER['HTTP_X_FORWARDED'])) { |
|
| 222 | + $proxy_ip = $_SERVER['HTTP_X_FORWARDED']; |
|
| 223 | + } elseif (!empty($_SERVER['HTTP_FORWARDED_FOR'])) { |
|
| 224 | + $proxy_ip = $_SERVER['HTTP_FORWARDED_FOR']; |
|
| 225 | + } elseif (!empty($_SERVER['HTTP_FORWARDED'])) { |
|
| 226 | + $proxy_ip = $_SERVER['HTTP_FORWARDED']; |
|
| 227 | + } elseif (!empty($_SERVER['HTTP_VIA'])) { |
|
| 228 | + $proxy_ip = $_SERVER['HTTP_VIA']; |
|
| 229 | + } elseif (!empty($_SERVER['HTTP_X_COMING_FROM'])) { |
|
| 230 | + $proxy_ip = $_SERVER['HTTP_X_COMING_FROM']; |
|
| 231 | + } elseif (!empty($_SERVER['HTTP_COMING_FROM'])) { |
|
| 232 | + $proxy_ip = $_SERVER['HTTP_COMING_FROM']; |
|
| 233 | + } |
|
| 234 | + $regs = array(); |
|
| 235 | + if (!empty($proxy_ip) && $is_ip = ereg('^([0-9]{1,3}\.){3,3}[0-9]{1,3}', $proxy_ip, $regs) && count($regs) > 0) { |
|
| 236 | + $the_IP = $regs[0]; |
|
| 237 | + } else { |
|
| 238 | + $the_IP = $_SERVER['REMOTE_ADDR']; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + return $the_IP; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Set the page's title, meta description and meta keywords |
|
| 246 | + * Datas are supposed to be sanitized |
|
| 247 | + * |
|
| 248 | + * @param string $pageTitle Page's Title |
|
| 249 | + * @param string $metaDescription Page's meta description |
|
| 250 | + * @param string $metaKeywords Page's meta keywords |
|
| 251 | + * @return void |
|
| 252 | + */ |
|
| 253 | + public static function setMetas($pageTitle = '', $metaDescription = '', $metaKeywords = '') |
|
| 254 | + { |
|
| 255 | + global $xoTheme, $xoTheme, $xoopsTpl; |
|
| 256 | + $xoopsTpl->assign('xoops_pagetitle', $pageTitle); |
|
| 257 | + if (isset($xoTheme) && is_object($xoTheme)) { |
|
| 258 | + if (!empty($metaKeywords)) { |
|
| 259 | + $xoTheme->addMeta('meta', 'keywords', $metaKeywords); |
|
| 260 | + } |
|
| 261 | + if (!empty($metaDescription)) { |
|
| 262 | + $xoTheme->addMeta('meta', 'description', $metaDescription); |
|
| 263 | + } |
|
| 264 | + } elseif (isset($xoopsTpl) && is_object($xoopsTpl)) { // Compatibility for old Xoops versions |
|
| 265 | + if (!empty($metaKeywords)) { |
|
| 266 | + $xoopsTpl->assign('xoops_meta_keywords', $metaKeywords); |
|
| 267 | + } |
|
| 268 | + if (!empty($metaDescription)) { |
|
| 269 | + $xoopsTpl->assign('xoops_meta_description', $metaDescription); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Send an email from a template to a list of recipients |
|
| 276 | + * |
|
| 277 | + * @param $tplName |
|
| 278 | + * @param array $recipients List of recipients |
|
| 279 | + * @param string $subject Email's subject |
|
| 280 | + * @param array $variables Varirables to give to the template |
|
| 281 | + * @return bool Result of the send |
|
| 282 | + * @internal param string $tpl_name Template's name |
|
| 283 | + */ |
|
| 284 | + public static function sendEmailFromTpl($tplName, $recipients, $subject, $variables) |
|
| 285 | + { |
|
| 286 | + global $xoopsConfig; |
|
| 287 | + require_once XOOPS_ROOT_PATH . '/class/xoopsmailer.php'; |
|
| 288 | + if (!is_array($recipients)) { |
|
| 289 | + if (trim($recipients) == '') { |
|
| 290 | + return false; |
|
| 291 | + } |
|
| 292 | + } else { |
|
| 293 | + if (count($recipients) == 0) { |
|
| 294 | + return false; |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | + if (function_exists('xoops_getMailer')) { |
|
| 298 | + $xoopsMailer =& xoops_getMailer(); |
|
| 299 | + } else { |
|
| 300 | + $xoopsMailer =& getMailer(); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + $xoopsMailer->useMail(); |
|
| 304 | + $xoopsMailer->setTemplateDir(XOOPS_ROOT_PATH . '/modules/' . self::MODULE_NAME . '/language/' . $xoopsConfig['language'] . '/mail_template'); |
|
| 305 | + $xoopsMailer->setTemplate($tplName); |
|
| 306 | + $xoopsMailer->setToEmails($recipients); |
|
| 307 | + // TODO: Change ! |
|
| 308 | + // $xoopsMailer->setFromEmail('[email protected]'); |
|
| 309 | + //$xoopsMailer->setFromName('MonSite'); |
|
| 310 | + $xoopsMailer->setSubject($subject); |
|
| 311 | + foreach ($variables as $key => $value) { |
|
| 312 | + $xoopsMailer->assign($key, $value); |
|
| 313 | + } |
|
| 314 | + $res = $xoopsMailer->send(); |
|
| 315 | + unset($xoopsMailer); |
|
| 316 | + $filename = XOOPS_UPLOAD_PATH . '/logmail_' . self::MODULE_NAME . '.php'; |
|
| 317 | + if (!file_exists($filename)) { |
|
| 318 | + $fp = @fopen($filename, 'a'); |
|
| 319 | + if ($fp) { |
|
| 320 | + fwrite($fp, '<?php exit(); ?>'); |
|
| 321 | + fclose($fp); |
|
| 322 | + } |
|
| 323 | + } |
|
| 324 | + $fp = @fopen($filename, 'a'); |
|
| 325 | + |
|
| 326 | + if ($fp) { |
|
| 327 | + fwrite($fp, str_repeat('-', 120) . "\n"); |
|
| 328 | + fwrite($fp, date('d/m/Y H:i:s') . "\n"); |
|
| 329 | + fwrite($fp, 'Template name : ' . $tplName . "\n"); |
|
| 330 | + fwrite($fp, 'Email subject : ' . $subject . "\n"); |
|
| 331 | + if (is_array($recipients)) { |
|
| 332 | + fwrite($fp, 'Recipient(s) : ' . implode(',', $recipients) . "\n"); |
|
| 333 | + } else { |
|
| 334 | + fwrite($fp, 'Recipient(s) : ' . $recipients . "\n"); |
|
| 335 | + } |
|
| 336 | + fwrite($fp, 'Transmited variables : ' . implode(',', $variables) . "\n"); |
|
| 337 | + fclose($fp); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + return $res; |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Remove module's cache |
|
| 345 | + */ |
|
| 346 | + public static function updateCache() |
|
| 347 | + { |
|
| 348 | + global $xoopsModule; |
|
| 349 | + $folder = $xoopsModule->getVar('dirname'); |
|
| 350 | + $tpllist = array(); |
|
| 351 | + require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php'; |
|
| 352 | + require_once XOOPS_ROOT_PATH . '/class/template.php'; |
|
| 353 | + $tplfile_handler = xoops_getHandler('tplfile'); |
|
| 354 | + $tpllist = $tplfile_handler->find(null, null, null, $folder); |
|
| 355 | + xoops_template_clear_module_cache($xoopsModule->getVar('mid')); // Clear module's blocks cache |
|
| 356 | + |
|
| 357 | + foreach ($tpllist as $onetemplate) { // Remove cache for each page. |
|
| 358 | + if ($onetemplate->getVar('tpl_type') === 'module') { |
|
| 359 | + // Note, I've been testing all the other methods (like the one of Smarty) and none of them run, that's why I have used this code |
|
| 360 | + $files_del = array(); |
|
| 361 | + $files_del = glob(XOOPS_CACHE_PATH . '/*' . $onetemplate->getVar('tpl_file') . '*'); |
|
| 362 | + if (count($files_del) > 0 && is_array($files_del)) { |
|
| 363 | + foreach ($files_del as $one_file) { |
|
| 364 | + if (is_file($one_file)) { |
|
| 365 | + unlink($one_file); |
|
| 366 | + } |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Redirect user with a message |
|
| 375 | + * |
|
| 376 | + * @param string $message message to display |
|
| 377 | + * @param string $url The place where to go |
|
| 378 | + * @param integer timeout Time to wait before to redirect |
|
| 379 | + */ |
|
| 380 | + public static function redirect($message = '', $url = 'index.php', $time = 2) |
|
| 381 | + { |
|
| 382 | + redirect_header($url, $time, $message); |
|
| 383 | + exit(); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Internal function used to get the handler of the current module |
|
| 388 | + * |
|
| 389 | + * @return object The module |
|
| 390 | + */ |
|
| 391 | + public static function getModule() |
|
| 392 | + { |
|
| 393 | + static $mymodule; |
|
| 394 | + if (!isset($mymodule)) { |
|
| 395 | + global $xoopsModule; |
|
| 396 | + if (isset($xoopsModule) && is_object($xoopsModule) && $xoopsModule->getVar('dirname') == REFERENCES_DIRNAME) { |
|
| 397 | + $mymodule =& $xoopsModule; |
|
| 398 | + } else { |
|
| 399 | + $hModule = xoops_getHandler('module'); |
|
| 400 | + $mymodule = $hModule->getByDirname(REFERENCES_DIRNAME); |
|
| 401 | + } |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + return $mymodule; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Returns the module's name (as defined by the user in the module manager) with cache |
|
| 409 | + * @return string Module's name |
|
| 410 | + */ |
|
| 411 | + public static function getModuleName() |
|
| 412 | + { |
|
| 413 | + static $moduleName; |
|
| 414 | + if (!isset($moduleName)) { |
|
| 415 | + $mymodule = self::getModule(); |
|
| 416 | + $moduleName = $mymodule->getVar('name'); |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + return $moduleName; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Create a title for the href tags inside html links |
|
| 424 | + * |
|
| 425 | + * @param string $title Text to use |
|
| 426 | + * @return string Formated text |
|
| 427 | + */ |
|
| 428 | + public static function makeHrefTitle($title) |
|
| 429 | + { |
|
| 430 | + $s = "\"'"; |
|
| 431 | + $r = ' '; |
|
| 432 | + |
|
| 433 | + return strtr($title, $s, $r); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * Retourne la liste des utilisateurs appartenants � un groupe |
|
| 438 | + * |
|
| 439 | + * @param int $groupId Searched group |
|
| 440 | + * @return array Array of XoopsUsers |
|
| 441 | + */ |
|
| 442 | + public static function getUsersFromGroup($groupId) |
|
| 443 | + { |
|
| 444 | + $users = array(); |
|
| 445 | + $member_handler = xoops_getHandler('member'); |
|
| 446 | + $users = $member_handler->getUsersByGroup($groupId, true); |
|
| 447 | + |
|
| 448 | + return $users; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Retourne les ID des utilisateurs faisant partie de plusieurs groupes |
|
| 453 | + * @param array $groups Les ID des groupes dont on recherche les ID utilisateurs |
|
| 454 | + * @return array Les ID utilisateurs |
|
| 455 | + */ |
|
| 456 | + public static function getUsersIdsFromGroups($groups) |
|
| 457 | + { |
|
| 458 | + $usersIds = array(); |
|
| 459 | + $member_handler = xoops_getHandler('member'); |
|
| 460 | + foreach ($groups as $groupId) { |
|
| 461 | + $temporaryGroup = array(); |
|
| 462 | + $temporaryGroup = $member_handler->getUsersByGroup($groupId); |
|
| 463 | + if (count($temporaryGroup) > 0) { |
|
| 464 | + $usersIds = array_merge($usersIds, $temporaryGroup); |
|
| 465 | + } |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + return array_unique($usersIds); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + /** |
|
| 472 | + * Retourne la liste des emails des utilisateurs membres d'un groupe |
|
| 473 | + * |
|
| 474 | + * @param $groupId |
|
| 475 | + * @return array Emails list |
|
| 476 | + * @internal param int $group_id Group's number |
|
| 477 | + */ |
|
| 478 | + public static function getEmailsFromGroup($groupId) |
|
| 479 | + { |
|
| 480 | + $ret = array(); |
|
| 481 | + $users = self::getUsersFromGroup($groupId); |
|
| 482 | + foreach ($users as $user) { |
|
| 483 | + $ret[] = $user->getVar('email'); |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + return $ret; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * V�rifie que l'utilisateur courant fait partie du groupe des administrateurs |
|
| 491 | + * |
|
| 492 | + * @return booleean Admin or not |
|
| 493 | + */ |
|
| 494 | + public static function isAdmin() |
|
| 495 | + { |
|
| 496 | + global $xoopsUser, $xoopsModule; |
|
| 497 | + if (is_object($xoopsUser)) { |
|
| 498 | + if (in_array(XOOPS_GROUP_ADMIN, $xoopsUser->getGroups())) { |
|
| 499 | + return true; |
|
| 500 | + } elseif (isset($xoopsModule) && $xoopsUser->isAdmin($xoopsModule->getVar('mid'))) { |
|
| 501 | + return true; |
|
| 502 | + } |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + return false; |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + /** |
|
| 509 | + * Returns the current date in the Mysql format |
|
| 510 | + * |
|
| 511 | + * @return string Date in the Mysql format |
|
| 512 | + */ |
|
| 513 | + public static function getCurrentSQLDate() |
|
| 514 | + { |
|
| 515 | + return date('Y-m-d'); // 2007-05-02 |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + public static function getCurrentSQLDateTime() |
|
| 519 | + { |
|
| 520 | + return date('Y-m-d H:i:s'); // 2007-05-02 |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Convert a Mysql date to the human's format |
|
| 525 | + * |
|
| 526 | + * @param string $date The date to convert |
|
| 527 | + * @param string $format |
|
| 528 | + * @return string The date in a human form |
|
| 529 | + */ |
|
| 530 | + public static function SQLDateToHuman($date, $format = 'l') |
|
| 531 | + { |
|
| 532 | + if ($date != '0000-00-00' && xoops_trim($date) != '') { |
|
| 533 | + return formatTimestamp(strtotime($date), $format); |
|
| 534 | + } else { |
|
| 535 | + return ''; |
|
| 536 | + } |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + /** |
|
| 540 | + * Convert a timestamp to a Mysql date |
|
| 541 | + * |
|
| 542 | + * @param integer $timestamp The timestamp to use |
|
| 543 | + * @return string The date in the Mysql format |
|
| 544 | + */ |
|
| 545 | + public static function timestampToMysqlDate($timestamp) |
|
| 546 | + { |
|
| 547 | + return date('Y-m-d', (int)$timestamp); |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * Conversion d'un dateTime Mysql en date lisible en fran�ais |
|
| 552 | + * @param $dateTime |
|
| 553 | + * @return bool|string |
|
| 554 | + */ |
|
| 555 | + public static function sqlDateTimeToFrench($dateTime) |
|
| 556 | + { |
|
| 557 | + return date('d/m/Y H:i:s', strtotime($dateTime)); |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * Convert a timestamp to a Mysql datetime form |
|
| 562 | + * @param integer $timestamp The timestamp to use |
|
| 563 | + * @return string The date and time in the Mysql format |
|
| 564 | + */ |
|
| 565 | + public static function timestampToMysqlDateTime($timestamp) |
|
| 566 | + { |
|
| 567 | + return date('Y-m-d H:i:s', $timestamp); |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * This function indicates if the current Xoops version needs to add asterisks to required fields in forms |
|
| 572 | + * |
|
| 573 | + * @return boolean Yes = we need to add them, false = no |
|
| 574 | + */ |
|
| 575 | + public static function needsAsterisk() |
|
| 576 | + { |
|
| 577 | + if (self::isX23()) { |
|
| 578 | + return false; |
|
| 579 | + } |
|
| 580 | + if (strpos(strtolower(XOOPS_VERSION), 'impresscms') !== false) { |
|
| 581 | + return false; |
|
| 582 | + } |
|
| 583 | + if (strpos(strtolower(XOOPS_VERSION), 'legacy') === false) { |
|
| 584 | + $xv = xoops_trim(str_replace('XOOPS ', '', XOOPS_VERSION)); |
|
| 585 | + if ((int)substr($xv, 4, 2) >= 17) { |
|
| 586 | + return false; |
|
| 587 | + } |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + return true; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * Mark the mandatory fields of a form with a star |
|
| 595 | + * |
|
| 596 | + * @param object $sform The form to modify |
|
| 597 | + * @return object The modified form |
|
| 598 | + * @internal param string $caracter The character to use to mark fields |
|
| 599 | + */ |
|
| 600 | + public static function &formMarkRequiredFields(&$sform) |
|
| 601 | + { |
|
| 602 | + if (self::needsAsterisk()) { |
|
| 603 | + $required = array(); |
|
| 604 | + foreach ($sform->getRequired() as $item) { |
|
| 605 | + $required[] = $item->_name; |
|
| 606 | + } |
|
| 607 | + $elements = array(); |
|
| 608 | + $elements = &$sform->getElements(); |
|
| 609 | + $cnt = count($elements); |
|
| 610 | + for ($i = 0; $i < $cnt; ++$i) { |
|
| 611 | + if (is_object($elements[$i]) && in_array($elements[$i]->_name, $required)) { |
|
| 612 | + $elements[$i]->_caption .= ' *'; |
|
| 613 | + } |
|
| 614 | + } |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + return $sform; |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * Create an html heading (from h1 to h6) |
|
| 622 | + * |
|
| 623 | + * @param string $title The text to use |
|
| 624 | + * @param integer $level Level to return |
|
| 625 | + * @return string The heading |
|
| 626 | + */ |
|
| 627 | + public static function htitle($title = '', $level = 1) |
|
| 628 | + { |
|
| 629 | + printf('<h%01d>%s</h%01d>', $level, $title, $level); |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * Create a unique upload filename |
|
| 634 | + * |
|
| 635 | + * @param string $folder The folder where the file will be saved |
|
| 636 | + * @param string $fileName Original filename (coming from the user) |
|
| 637 | + * @param boolean $trimName Do we need to create a short unique name ? |
|
| 638 | + * @return string The unique filename to use (with its extension) |
|
| 639 | + */ |
|
| 640 | + public static function createUploadName($folder, $fileName, $trimName = false) |
|
| 641 | + { |
|
| 642 | + $workingfolder = $folder; |
|
| 643 | + if (substr($workingfolder, strlen($workingfolder) - 1, 1) !== '/') { |
|
| 644 | + $workingfolder .= '/'; |
|
| 645 | + } |
|
| 646 | + $ext = basename($fileName); |
|
| 647 | + $ext = explode('.', $ext); |
|
| 648 | + $ext = '.' . $ext[count($ext) - 1]; |
|
| 649 | + $true = true; |
|
| 650 | + while ($true) { |
|
| 651 | + $ipbits = explode('.', $_SERVER['REMOTE_ADDR']); |
|
| 652 | + list($usec, $sec) = explode(' ', microtime()); |
|
| 653 | + $usec = (integer)($usec * 65536); |
|
| 654 | + $sec = ((integer)$sec) & 0xFFFF; |
|
| 655 | + |
|
| 656 | + if ($trimName) { |
|
| 657 | + $uid = sprintf('%06x%04x%04x', ($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec); |
|
| 658 | + } else { |
|
| 659 | + $uid = sprintf('%08x-%04x-%04x', ($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec); |
|
| 660 | + } |
|
| 661 | + if (!file_exists($workingfolder . $uid . $ext)) { |
|
| 662 | + $true = false; |
|
| 663 | + } |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + return $uid . $ext; |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + /** |
|
| 670 | + * Replace html entities with their ASCII equivalent |
|
| 671 | + * |
|
| 672 | + * @param string $chaine The string undecode |
|
| 673 | + * @return string The undecoded string |
|
| 674 | + */ |
|
| 675 | + public static function unhtml($chaine) |
|
| 676 | + { |
|
| 677 | + $search = $replace = array(); |
|
| 678 | + $chaine = html_entity_decode($chaine); |
|
| 679 | + |
|
| 680 | + for ($i = 0; $i <= 255; ++$i) { |
|
| 681 | + $search[] = '&#' . $i . ';'; |
|
| 682 | + $replace[] = chr($i); |
|
| 683 | + } |
|
| 684 | 684 | $replace[]='...'; $search[]='…'; |
| 685 | 685 | $replace[]="'"; $search[]='‘'; |
| 686 | 686 | $replace[]="'"; $search[]= '’'; |
@@ -806,765 +806,765 @@ discard block |
||
| 806 | 806 | $replace[]='Ý'; $search[]='Ý'; |
| 807 | 807 | $replace[]='ÿ'; $search[]='ÿ'; |
| 808 | 808 | $replace[]='Ÿ'; $search[]='Ÿ'; |
| 809 | - $chaine = str_replace($search, $replace, $chaine); |
|
| 810 | - |
|
| 811 | - return $chaine; |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - /** |
|
| 815 | - * Création d'une titre pour être utilisé par l'url rewriting |
|
| 816 | - * |
|
| 817 | - * @param string $content Le texte à utiliser pour créer l'url |
|
| 818 | - * @param integer $urw La limite basse pour créer les mots |
|
| 819 | - * @return string Le texte à utiliser pour l'url |
|
| 820 | - * Note, some parts are from Solo's code |
|
| 821 | - */ |
|
| 822 | - public static function makeSeoUrl($content, $urw = 1) |
|
| 823 | - { |
|
| 824 | - $s = "ÀÁÂÃÄÅÒÓÔÕÖØÈÉÊËÇÌÍÎÏÙÚÛÜŸÑàáâãäåòóôõöøèéêëçìíîïùúûüÿñ '()"; |
|
| 825 | - $r = 'AAAAAAOOOOOOEEEECIIIIUUUUYNaaaaaaooooooeeeeciiiiuuuuyn----'; |
|
| 826 | - $content = self::unhtml($content); // First, remove html entities |
|
| 827 | - $content = strtr($content, $s, $r); |
|
| 828 | - $content = strip_tags($content); |
|
| 829 | - $content = strtolower($content); |
|
| 830 | - $content = htmlentities($content); // TODO: Vérifier |
|
| 831 | - $content = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde);/', '$1', $content); |
|
| 832 | - $content = html_entity_decode($content); |
|
| 833 | - $content = eregi_replace('quot', ' ', $content); |
|
| 834 | - $content = eregi_replace("'", ' ', $content); |
|
| 835 | - $content = eregi_replace('-', ' ', $content); |
|
| 836 | - $content = eregi_replace('[[:punct:]]', '', $content); |
|
| 837 | - // Selon option mais attention au fichier .htaccess ! |
|
| 838 | - // $content = eregi_replace('[[:digit:]]','', $content); |
|
| 839 | - $content = preg_replace('/[^a-z|A-Z|0-9]/', '-', $content); |
|
| 840 | - |
|
| 841 | - $words = explode(' ', $content); |
|
| 842 | - $keywords = ''; |
|
| 843 | - foreach ($words as $word) { |
|
| 844 | - if (strlen($word) >= $urw) { |
|
| 845 | - $keywords .= '-' . trim($word); |
|
| 846 | - } |
|
| 847 | - } |
|
| 848 | - if (!$keywords) { |
|
| 849 | - $keywords = '-'; |
|
| 850 | - } |
|
| 851 | - // Supprime les tirets en double |
|
| 852 | - $keywords = str_replace('---', '-', $keywords); |
|
| 853 | - $keywords = str_replace('--', '-', $keywords); |
|
| 854 | - // Supprime un éventuel tiret à la fin de la chaine |
|
| 855 | - if (substr($keywords, strlen($keywords) - 1, 1) == '-') { |
|
| 856 | - $keywords = substr($keywords, 0, strlen($keywords) - 1); |
|
| 857 | - } |
|
| 858 | - |
|
| 859 | - return $keywords; |
|
| 860 | - } |
|
| 861 | - |
|
| 862 | - /** |
|
| 863 | - * Create the meta keywords based on the content |
|
| 864 | - * |
|
| 865 | - * @param string $content Content from which we have to create metakeywords |
|
| 866 | - * @return string The list of meta keywords |
|
| 867 | - */ |
|
| 868 | - public static function createMetaKeywords($content) |
|
| 869 | - { |
|
| 870 | - $keywordscount = REFERENCES_METAGEN_MAX_KEYWORDS; |
|
| 871 | - $keywordsorder = REFERENCES_METAGEN_KEYWORDS_ORDER; |
|
| 872 | - |
|
| 873 | - $tmp = array(); |
|
| 874 | - // Search for the "Minimum keyword length" |
|
| 875 | - // TODO: Remplacer references_keywords_limit par une constante |
|
| 876 | - if (isset($_SESSION['references_keywords_limit'])) { |
|
| 877 | - $limit = $_SESSION['references_keywords_limit']; |
|
| 878 | - } else { |
|
| 879 | - $config_handler = xoops_getHandler('config'); |
|
| 880 | - $xoopsConfigSearch =& $config_handler->getConfigsByCat(XOOPS_CONF_SEARCH); |
|
| 881 | - $limit = $xoopsConfigSearch['keyword_min']; |
|
| 882 | - $_SESSION['references_keywords_limit'] = $limit; |
|
| 883 | - } |
|
| 884 | - $myts = MyTextSanitizer::getInstance(); |
|
| 885 | - $content = str_replace('<br>', ' ', $content); |
|
| 886 | - $content = $myts->undoHtmlSpecialChars($content); |
|
| 887 | - $content = strip_tags($content); |
|
| 888 | - $content = strtolower($content); |
|
| 889 | - $search_pattern = array(' ', "\t", "\r\n", "\r", "\n", ',', '.', "'", ';', ':', ')', '(', '"', '?', '!', '{', '}', '[', ']', '<', '>', '/', '+', '-', '_', '\\', '*'); |
|
| 890 | - $replace_pattern = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); |
|
| 891 | - $content = str_replace($search_pattern, $replace_pattern, $content); |
|
| 892 | - $keywords = explode(' ', $content); |
|
| 893 | - switch ($keywordsorder) { |
|
| 894 | - case 0: // Ordre d'apparition dans le texte |
|
| 895 | - $keywords = array_unique($keywords); |
|
| 896 | - break; |
|
| 897 | - case 1: // Ordre de fréquence des mots |
|
| 898 | - $keywords = array_count_values($keywords); |
|
| 899 | - asort($keywords); |
|
| 900 | - $keywords = array_keys($keywords); |
|
| 901 | - break; |
|
| 902 | - case 2: // Ordre inverse de la fréquence des mots |
|
| 903 | - $keywords = array_count_values($keywords); |
|
| 904 | - arsort($keywords); |
|
| 905 | - $keywords = array_keys($keywords); |
|
| 906 | - break; |
|
| 907 | - } |
|
| 908 | - // Remove black listed words |
|
| 909 | - if (xoops_trim(self::getModuleOption('metagen_blacklist')) != '') { |
|
| 910 | - $metagen_blacklist = str_replace("\r", '', self::getModuleOption('metagen_blacklist')); |
|
| 911 | - $metablack = explode("\n", $metagen_blacklist); |
|
| 912 | - array_walk($metablack, 'trim'); |
|
| 913 | - $keywords = array_diff($keywords, $metablack); |
|
| 914 | - } |
|
| 915 | - |
|
| 916 | - foreach ($keywords as $keyword) { |
|
| 917 | - if (strlen($keyword) >= $limit && !is_numeric($keyword)) { |
|
| 918 | - $tmp[] = $keyword; |
|
| 919 | - } |
|
| 920 | - } |
|
| 921 | - $tmp = array_slice($tmp, 0, $keywordscount); |
|
| 922 | - if (count($tmp) > 0) { |
|
| 923 | - return implode(',', $tmp); |
|
| 924 | - } else { |
|
| 925 | - if (!isset($config_handler) || !is_object($config_handler)) { |
|
| 926 | - $config_handler = xoops_getHandler('config'); |
|
| 927 | - } |
|
| 928 | - $xoopsConfigMetaFooter =& $config_handler->getConfigsByCat(XOOPS_CONF_METAFOOTER); |
|
| 929 | - if (isset($xoopsConfigMetaFooter['meta_keywords'])) { |
|
| 930 | - return $xoopsConfigMetaFooter['meta_keywords']; |
|
| 931 | - } else { |
|
| 932 | - return ''; |
|
| 933 | - } |
|
| 934 | - } |
|
| 935 | - } |
|
| 936 | - |
|
| 937 | - /** |
|
| 938 | - * Fonction chargée de gérer l'upload |
|
| 939 | - * |
|
| 940 | - * @param integer $indice L'indice du fichier à télécharger |
|
| 941 | - * @param string $dstpath |
|
| 942 | - * @param null $mimeTypes |
|
| 943 | - * @param null $uploadMaxSize |
|
| 944 | - * @param null $maxWidth |
|
| 945 | - * @param null $maxHeight |
|
| 946 | - * @return mixed True si l'upload s'est bien déroulé sinon le message d'erreur correspondant |
|
| 947 | - */ |
|
| 948 | - public static function uploadFile($indice, $dstpath = XOOPS_UPLOAD_PATH, $mimeTypes = null, $uploadMaxSize = null, $maxWidth = null, $maxHeight = null) |
|
| 949 | - { |
|
| 950 | - require_once XOOPS_ROOT_PATH . '/class/uploader.php'; |
|
| 951 | - global $destname; |
|
| 952 | - if (isset($_POST['xoops_upload_file'])) { |
|
| 953 | - require_once XOOPS_ROOT_PATH . '/class/uploader.php'; |
|
| 954 | - $fldname = ''; |
|
| 955 | - $fldname = $_FILES[$_POST['xoops_upload_file'][$indice]]; |
|
| 956 | - $fldname = get_magic_quotes_gpc() ? stripslashes($fldname['name']) : $fldname['name']; |
|
| 957 | - if (xoops_trim($fldname != '')) { |
|
| 958 | - $destname = self::createUploadName($dstpath, $fldname, true); |
|
| 959 | - if ($mimeTypes === null) { |
|
| 960 | - $permittedtypes = explode("\n", str_replace("\r", '', self::getModuleOption('mimetypes'))); |
|
| 961 | - array_walk($permittedtypes, 'trim'); |
|
| 962 | - } else { |
|
| 963 | - $permittedtypes = $mimeTypes; |
|
| 964 | - } |
|
| 965 | - if ($uploadMaxSize === null) { |
|
| 966 | - $uploadSize = self::getModuleOption('maxuploadsize'); |
|
| 967 | - } else { |
|
| 968 | - $uploadSize = $uploadMaxSize; |
|
| 969 | - } |
|
| 970 | - $uploader = new XoopsMediaUploader($dstpath, $permittedtypes, $uploadSize, $maxWidth, $maxHeight); |
|
| 971 | - //$uploader->allowUnknownTypes = true; |
|
| 972 | - $uploader->setTargetFileName($destname); |
|
| 973 | - if ($uploader->fetchMedia($_POST['xoops_upload_file'][$indice])) { |
|
| 974 | - if ($uploader->upload()) { |
|
| 975 | - return true; |
|
| 976 | - } else { |
|
| 977 | - return _ERRORS . ' ' . htmlentities($uploader->getErrors()); |
|
| 978 | - } |
|
| 979 | - } else { |
|
| 980 | - return htmlentities($uploader->getErrors()); |
|
| 981 | - } |
|
| 982 | - } else { |
|
| 983 | - return false; |
|
| 984 | - } |
|
| 985 | - } else { |
|
| 986 | - return false; |
|
| 987 | - } |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - /** |
|
| 991 | - * Resize a Picture to some given dimensions (using the wideImage library) |
|
| 992 | - * |
|
| 993 | - * @param string $src_path Picture's source |
|
| 994 | - * @param string $dst_path Picture's destination |
|
| 995 | - * @param integer $param_width Maximum picture's width |
|
| 996 | - * @param integer $param_height Maximum picture's height |
|
| 997 | - * @param boolean $keep_original Do we have to keep the original picture ? |
|
| 998 | - * @param string $fit Resize mode (see the wideImage library for more information) |
|
| 999 | - * @return bool |
|
| 1000 | - */ |
|
| 1001 | - public static function resizePicture($src_path, $dst_path, $param_width, $param_height, $keep_original = false, $fit = 'inside') |
|
| 1002 | - { |
|
| 1003 | - // require_once self::MODULE_PATH . 'class/wideimage/WideImage.php'; |
|
| 1004 | - $img = WideImage::load($src_path); |
|
| 1005 | - $result = $img->resize($param_width, $param_height, $fit); |
|
| 1006 | - $result->saveToFile($dst_path, null); |
|
| 1007 | - if (!$keep_original) { |
|
| 1008 | - @unlink($src_path); |
|
| 1009 | - } |
|
| 1010 | - |
|
| 1011 | - return true; |
|
| 1012 | - } |
|
| 1013 | - |
|
| 1014 | - /** |
|
| 1015 | - * Déclenchement d'une alerte Xoops suite à un évènement |
|
| 1016 | - * |
|
| 1017 | - * @param string $category La catégorie de l'évènement |
|
| 1018 | - * @param integer $itemId L'ID de l'élément (trop général pour être décris précisément) |
|
| 1019 | - * @param unknown_type $event L'évènement qui est déclencé |
|
| 1020 | - * @param unknown_type $tags Les variables à passer au template |
|
| 1021 | - */ |
|
| 1022 | - public static function notify($category, $itemId, $event, $tags) |
|
| 1023 | - { |
|
| 1024 | - $notification_handler = xoops_getHandler('notification'); |
|
| 1025 | - $tags['X_MODULE_URL'] = REFERENCES_URL; |
|
| 1026 | - $notification_handler->triggerEvent($category, $itemId, $event, $tags); |
|
| 1027 | - } |
|
| 1028 | - |
|
| 1029 | - /** |
|
| 1030 | - * Ajoute des jours à une date et retourne la nouvelle date au format Date de Mysql |
|
| 1031 | - * |
|
| 1032 | - * @param int $duration |
|
| 1033 | - * @param integer $startingDate Date de départ (timestamp) |
|
| 1034 | - * @return bool|string |
|
| 1035 | - * @internal param int $durations Durée en jours |
|
| 1036 | - */ |
|
| 1037 | - public static function addDaysToDate($duration = 1, $startingDate = 0) |
|
| 1038 | - { |
|
| 1039 | - if ($startingDate == 0) { |
|
| 1040 | - $startingDate = time(); |
|
| 1041 | - } |
|
| 1042 | - $endingDate = $startingDate + ($duration * 86400); |
|
| 1043 | - |
|
| 1044 | - return date('Y-m-d', $endingDate); |
|
| 1045 | - } |
|
| 1046 | - |
|
| 1047 | - /** |
|
| 1048 | - * Retourne un breadcrumb en fonction des paramètres passés et en partant (d'office) de la racine du module |
|
| 1049 | - * |
|
| 1050 | - * @param array $path Le chemin complet (excepté la racine) du breadcrumb sous la forme clé=url valeur=titre |
|
| 1051 | - * @param string $raquo Le séparateur par défaut à utiliser |
|
| 1052 | - * @return string le breadcrumb |
|
| 1053 | - */ |
|
| 1054 | - public static function breadcrumb($path, $raquo = ' » ') |
|
| 1055 | - { |
|
| 1056 | - $breadcrumb = ''; |
|
| 1057 | - $workingBreadcrumb = array(); |
|
| 1058 | - if (is_array($path)) { |
|
| 1059 | - $moduleName = self::getModuleName(); |
|
| 1060 | - $workingBreadcrumb[] = "<a href='" . REFERENCES_URL . "' title='" . self::makeHrefTitle($moduleName) . "'>" . $moduleName . '</a>'; |
|
| 1061 | - foreach ($path as $url => $title) { |
|
| 1062 | - $workingBreadcrumb[] = "<a href='" . $url . "'>" . $title . '</a>'; |
|
| 1063 | - } |
|
| 1064 | - $cnt = count($workingBreadcrumb); |
|
| 1065 | - for ($i = 0; $i < $cnt; ++$i) { |
|
| 1066 | - if ($i == $cnt - 1) { |
|
| 1067 | - $workingBreadcrumb[$i] = strip_tags($workingBreadcrumb[$i]); |
|
| 1068 | - } |
|
| 1069 | - } |
|
| 1070 | - $breadcrumb = implode($raquo, $workingBreadcrumb); |
|
| 1071 | - } |
|
| 1072 | - |
|
| 1073 | - return $breadcrumb; |
|
| 1074 | - } |
|
| 1075 | - |
|
| 1076 | - public static function close_tags($string) |
|
| 1077 | - { |
|
| 1078 | - // match opened tags |
|
| 1079 | - if (preg_match_all('/<([a-z\:\-]+)[^\/]>/', $string, $start_tags)) { |
|
| 1080 | - $start_tags = $start_tags[1]; |
|
| 1081 | - |
|
| 1082 | - // match closed tags |
|
| 1083 | - if (preg_match_all('/<\/([a-z]+)>/', $string, $end_tags)) { |
|
| 1084 | - $complete_tags = array(); |
|
| 1085 | - $end_tags = $end_tags[1]; |
|
| 1086 | - |
|
| 1087 | - foreach ($start_tags as $key => $val) { |
|
| 1088 | - $posb = array_search($val, $end_tags); |
|
| 1089 | - if (is_int($posb)) { |
|
| 1090 | - unset($end_tags[$posb]); |
|
| 1091 | - } else { |
|
| 1092 | - $complete_tags[] = $val; |
|
| 1093 | - } |
|
| 1094 | - } |
|
| 1095 | - } else { |
|
| 1096 | - $complete_tags = $start_tags; |
|
| 1097 | - } |
|
| 1098 | - |
|
| 1099 | - $complete_tags = array_reverse($complete_tags); |
|
| 1100 | - for ($i = 0; $i < count($complete_tags); ++$i) { |
|
| 1101 | - $string .= '</' . $complete_tags[$i] . '>'; |
|
| 1102 | - } |
|
| 1103 | - } |
|
| 1104 | - |
|
| 1105 | - return $string; |
|
| 1106 | - } |
|
| 1107 | - |
|
| 1108 | - public static function truncate_tagsafe($string, $length = 80, $etc = '...', $break_words = false) |
|
| 1109 | - { |
|
| 1110 | - if ($length == 0) { |
|
| 1111 | - return ''; |
|
| 1112 | - } |
|
| 1113 | - |
|
| 1114 | - if (strlen($string) > $length) { |
|
| 1115 | - $length -= strlen($etc); |
|
| 1116 | - if (!$break_words) { |
|
| 1117 | - $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1)); |
|
| 1118 | - $string = preg_replace('/<[^>]*$/', '', $string); |
|
| 1119 | - $string = self::close_tags($string); |
|
| 1120 | - } |
|
| 1121 | - |
|
| 1122 | - return $string . $etc; |
|
| 1123 | - } else { |
|
| 1124 | - return $string; |
|
| 1125 | - } |
|
| 1126 | - } |
|
| 1127 | - |
|
| 1128 | - /** |
|
| 1129 | - * Create an infotip |
|
| 1130 | - * @param $text |
|
| 1131 | - * @return string |
|
| 1132 | - */ |
|
| 1133 | - public static function makeInfotips($text) |
|
| 1134 | - { |
|
| 1135 | - $ret = ''; |
|
| 1136 | - $infotips = self::getModuleOption('infotips'); |
|
| 1137 | - if ($infotips > 0) { |
|
| 1138 | - $myts = MyTextSanitizer::getInstance(); |
|
| 1139 | - $ret = $myts->htmlSpecialChars(substr(strip_tags($text), 0, $infotips)); |
|
| 1140 | - } |
|
| 1141 | - |
|
| 1142 | - return $ret; |
|
| 1143 | - } |
|
| 1144 | - |
|
| 1145 | - /** |
|
| 1146 | - * Retourne le type Mime d'un fichier en utilisant d'abord finfo puis mime_content |
|
| 1147 | - * |
|
| 1148 | - * @param string $filename Le fichier (avec son chemin d'accès complet) dont on veut connaître le type mime |
|
| 1149 | - * @return string |
|
| 1150 | - */ |
|
| 1151 | - public static function getMimeType($filename) |
|
| 1152 | - { |
|
| 1153 | - if (function_exists('finfo_open')) { |
|
| 1154 | - $pathToMagic = REFERENCES_PATH . 'mime/magic'; |
|
| 1155 | - $finfo = new finfo(FILEINFO_MIME, $pathToMagic); |
|
| 1156 | - $mimetype = $finfo->file($filename); |
|
| 1157 | - finfo_close($finfo); |
|
| 1158 | - unset($finfo); |
|
| 1159 | - |
|
| 1160 | - return $mimetype; |
|
| 1161 | - } else { |
|
| 1162 | - if (function_exists('mime_content_type')) { |
|
| 1163 | - return mime_content_type($filename); |
|
| 1164 | - } else { |
|
| 1165 | - return ''; |
|
| 1166 | - } |
|
| 1167 | - } |
|
| 1168 | - } |
|
| 1169 | - |
|
| 1170 | - /** |
|
| 1171 | - * Retourne une liste d'objets XoopsUsers à partir d'une liste d'identifiants |
|
| 1172 | - * |
|
| 1173 | - * @param array $xoopsUsersIDs La liste des ID |
|
| 1174 | - * @return array Les objets XoopsUsers |
|
| 1175 | - */ |
|
| 1176 | - public static function getUsersFromIds($xoopsUsersIDs) |
|
| 1177 | - { |
|
| 1178 | - $users = array(); |
|
| 1179 | - if (is_array($xoopsUsersIDs) && count($xoopsUsersIDs) > 0) { |
|
| 1180 | - $xoopsUsersIDs = array_unique($xoopsUsersIDs); |
|
| 1181 | - sort($xoopsUsersIDs); |
|
| 1182 | - if (count($xoopsUsersIDs) > 0) { |
|
| 1183 | - $member_handler = xoops_getHandler('user'); |
|
| 1184 | - $criteria = new Criteria('uid', '(' . implode(',', $xoopsUsersIDs) . ')', 'IN'); |
|
| 1185 | - $criteria->setSort('uid'); |
|
| 1186 | - $users = $member_handler->getObjects($criteria, true); |
|
| 1187 | - } |
|
| 1188 | - } |
|
| 1189 | - |
|
| 1190 | - return $users; |
|
| 1191 | - } |
|
| 1192 | - |
|
| 1193 | - /** |
|
| 1194 | - * Retourne l'ID de l'utilisateur courant (s'il est connecté) |
|
| 1195 | - * @return integer L'uid ou 0 |
|
| 1196 | - */ |
|
| 1197 | - public static function getCurrentUserID() |
|
| 1198 | - { |
|
| 1199 | - global $xoopsUser; |
|
| 1200 | - $uid = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0; |
|
| 1201 | - |
|
| 1202 | - return $uid; |
|
| 1203 | - } |
|
| 1204 | - |
|
| 1205 | - /** |
|
| 1206 | - * Retourne la liste des groupes de l'utilisateur courant (avec cache) |
|
| 1207 | - * @param int $uid |
|
| 1208 | - * @return array Les ID des groupes auquel l'utilisateur courant appartient |
|
| 1209 | - */ |
|
| 1210 | - public static function getMemberGroups($uid = 0) |
|
| 1211 | - { |
|
| 1212 | - static $buffer = array(); |
|
| 1213 | - if ($uid == 0) { |
|
| 1214 | - $uid = self::getCurrentUserID(); |
|
| 1215 | - } |
|
| 1216 | - |
|
| 1217 | - if (is_array($buffer) && count($buffer) > 0 && isset($buffer[$uid])) { |
|
| 1218 | - return $buffer[$uid]; |
|
| 1219 | - } else { |
|
| 1220 | - if ($uid > 0) { |
|
| 1221 | - $member_handler = xoops_getHandler('member'); |
|
| 1222 | - $buffer[$uid] = $member_handler->getGroupsByUser($uid, false); // Renvoie un tableau d'ID (de groupes) |
|
| 1223 | - } else { |
|
| 1224 | - $buffer[$uid] = array(XOOPS_GROUP_ANONYMOUS); |
|
| 1225 | - } |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - return $buffer[$uid]; |
|
| 1229 | - } |
|
| 1230 | - |
|
| 1231 | - /** |
|
| 1232 | - * Indique si l'utilisateur courant fait partie d'une groupe donné (avec gestion de cache) |
|
| 1233 | - * |
|
| 1234 | - * @param integer $group Groupe recherché |
|
| 1235 | - * @param int $uid |
|
| 1236 | - * @return bool vrai si l'utilisateur fait partie du groupe, faux sinon |
|
| 1237 | - */ |
|
| 1238 | - public static function isMemberOfGroup($group = 0, $uid = 0) |
|
| 1239 | - { |
|
| 1240 | - static $buffer = array(); |
|
| 1241 | - $retval = false; |
|
| 1242 | - if ($uid == 0) { |
|
| 1243 | - $uid = self::getCurrentUserID(); |
|
| 1244 | - } |
|
| 1245 | - if (is_array($buffer) && array_key_exists($group, $buffer)) { |
|
| 1246 | - $retval = $buffer[$group]; |
|
| 1247 | - } else { |
|
| 1248 | - $member_handler = xoops_getHandler('member'); |
|
| 1249 | - $groups = $member_handler->getGroupsByUser($uid, false); // Renvoie un tableau d'ID (de groupes) |
|
| 1250 | - $retval = in_array($group, $groups); |
|
| 1251 | - $buffer[$group] = $retval; |
|
| 1252 | - } |
|
| 1253 | - |
|
| 1254 | - return $retval; |
|
| 1255 | - } |
|
| 1256 | - |
|
| 1257 | - /** |
|
| 1258 | - * Indique si un utilisateur fait partie de plusieurs groupes |
|
| 1259 | - * La fonction renvoie vrai dès qu'on trouve que le membre fait partir d'un des groupes |
|
| 1260 | - * @param array $groups La liste des groupes à vérifier |
|
| 1261 | - * @param integer $uid L'ID de l'utilisateur |
|
| 1262 | - * @return boolean |
|
| 1263 | - */ |
|
| 1264 | - public static function isMemberOfGroups($groups, $uid = 0) |
|
| 1265 | - { |
|
| 1266 | - if (count($groups) == 0) { |
|
| 1267 | - return false; |
|
| 1268 | - } |
|
| 1269 | - if ($uid == 0) { |
|
| 1270 | - $uid = self::getCurrentUserID(); |
|
| 1271 | - } |
|
| 1272 | - foreach ($groups as $groupId) { |
|
| 1273 | - if (self::isMemberOfGroup($groupId, $uid)) { |
|
| 1274 | - return true; |
|
| 1275 | - } |
|
| 1276 | - } |
|
| 1277 | - |
|
| 1278 | - return false; |
|
| 1279 | - } |
|
| 1280 | - |
|
| 1281 | - /** |
|
| 1282 | - * Retourne le nombre total de groupes (y compris les anonymes) |
|
| 1283 | - */ |
|
| 1284 | - public static function getGroupsCount() |
|
| 1285 | - { |
|
| 1286 | - static $cache = -1; |
|
| 1287 | - if ($cache == -1) { |
|
| 1288 | - global $xoopsDB; |
|
| 1289 | - $sql = 'SELECT COUNT(*) FROM ' . $xoopsDB->prefix('groups'); |
|
| 1290 | - $result = $xoopsDB->query($sql); |
|
| 1291 | - if (!$result) { |
|
| 1292 | - return false; |
|
| 1293 | - } |
|
| 1294 | - list($cache) = $xoopsDB->fetchRow($result); |
|
| 1295 | - } |
|
| 1296 | - |
|
| 1297 | - return $cache; |
|
| 1298 | - } |
|
| 1299 | - |
|
| 1300 | - /** |
|
| 1301 | - * Fonction chargée de vérifier qu'un répertoire existe, qu'on peut écrire dedans et création d'un fichier index.html |
|
| 1302 | - * |
|
| 1303 | - * @param string $folder Le chemin complet du répertoire à vérifier |
|
| 1304 | - * @return void |
|
| 1305 | - */ |
|
| 1306 | - public static function prepareFolder($folder) |
|
| 1307 | - { |
|
| 1308 | - if (!is_dir($folder)) { |
|
| 1309 | - mkdir($folder, 0777); |
|
| 1310 | - file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
|
| 1311 | - } else { |
|
| 1312 | - if (!is_writable($folder)) { |
|
| 1313 | - chmod($folder, 0777); |
|
| 1314 | - } |
|
| 1315 | - } |
|
| 1316 | - } |
|
| 1317 | - |
|
| 1318 | - /** |
|
| 1319 | - * Load a language file |
|
| 1320 | - * |
|
| 1321 | - * @param string $languageFile The required language file |
|
| 1322 | - * @param string $defaultExtension Default extension to use |
|
| 1323 | - */ |
|
| 1324 | - public static function loadLanguageFile($languageFile, $defaultExtension = '.php') |
|
| 1325 | - { |
|
| 1326 | - global $xoopsConfig; |
|
| 1327 | - $root = self::MODULE_PATH; |
|
| 1328 | - if (strstr($languageFile, $defaultExtension) === false) { |
|
| 1329 | - $languageFile .= $defaultExtension; |
|
| 1330 | - } |
|
| 1331 | - if (file_exists($root . 'language' . DIRECTORY_SEPARATOR . $xoopsConfig['language'] . DIRECTORY_SEPARATOR . $languageFile)) { |
|
| 1332 | - require_once $root . 'language' . DIRECTORY_SEPARATOR . $xoopsConfig['language'] . DIRECTORY_SEPARATOR . $languageFile; |
|
| 1333 | - } else { // Fallback |
|
| 1334 | - require_once $root . 'language' . DIRECTORY_SEPARATOR . 'english' . DIRECTORY_SEPARATOR . $languageFile; |
|
| 1335 | - } |
|
| 1336 | - } |
|
| 1337 | - |
|
| 1338 | - /** |
|
| 1339 | - * Création d'une vignette en tenant compte des options du module concernant le mode de création des vignettes |
|
| 1340 | - * |
|
| 1341 | - * @param string $src_path |
|
| 1342 | - * @param string $dst_path |
|
| 1343 | - * @param integer $param_width |
|
| 1344 | - * @param integer $param_height |
|
| 1345 | - * @param boolean $keep_original |
|
| 1346 | - * @param string $fit Utilisé uniquement pour la méthode 0 (redimensionnement) |
|
| 1347 | - * @return boolean |
|
| 1348 | - */ |
|
| 1349 | - public static function createThumb($src_path, $dst_path, $param_width, $param_height, $keep_original = false, $fit = 'inside') |
|
| 1350 | - { |
|
| 1351 | - // require_once self::MODULE_PATH . 'class/wideimage/WideImage.php'; |
|
| 1352 | - $resize = true; |
|
| 1353 | - // On commence par vérifier que l'image originale n'est pas plus petite que les dimensions demandées auquel cas il n'y a rien à faire |
|
| 1354 | - $pictureDimensions = getimagesize($src_path); |
|
| 1355 | - if (is_array($pictureDimensions)) { |
|
| 1356 | - $pictureWidth = $pictureDimensions[0]; |
|
| 1357 | - $pictureHeight = $pictureDimensions[1]; |
|
| 1358 | - if ($pictureWidth < $param_width && $pictureHeight < $param_height) { |
|
| 1359 | - $resize = false; |
|
| 1360 | - } |
|
| 1361 | - } |
|
| 1362 | - $img = WideImage::load($src_path); |
|
| 1363 | - if ($resize) { // L'image est suffisament grande pour être réduite |
|
| 1364 | - $thumbMethod = 0; |
|
| 1365 | - if ($thumbMethod == 0) { // Redimensionnement de l'image |
|
| 1366 | - $result = $img->resize($param_width, $param_height, $fit); |
|
| 1367 | - } else { // Récupération d'une partie de l'image depuis le centre |
|
| 1368 | - // Calcul de left et top |
|
| 1369 | - $left = $top = 0; |
|
| 1370 | - if (is_array($pictureDimensions)) { |
|
| 1371 | - if ($pictureWidth > $param_width) { |
|
| 1372 | - $left = (int)(($pictureWidth / 2) - ($param_width / 2)); |
|
| 1373 | - } |
|
| 1374 | - if ($pictureHeight > $param_height) { |
|
| 1375 | - $top = (int)(($pictureHeight / 2) - ($param_height / 2)); |
|
| 1376 | - } |
|
| 1377 | - } |
|
| 1378 | - $result = $img->crop($left, $top, $param_width, $param_height); |
|
| 1379 | - } |
|
| 1380 | - $result->saveToFile($dst_path, null); |
|
| 1381 | - } else { |
|
| 1382 | - if ($src_path != $dst_path) { |
|
| 1383 | - @copy($src_path, $dst_path); |
|
| 1384 | - } |
|
| 1385 | - } |
|
| 1386 | - if (!$keep_original) { |
|
| 1387 | - @unlink($src_path); |
|
| 1388 | - } |
|
| 1389 | - |
|
| 1390 | - return true; |
|
| 1391 | - } |
|
| 1392 | - |
|
| 1393 | - /** |
|
| 1394 | - * Create the <option> of an html select |
|
| 1395 | - * |
|
| 1396 | - * @param array $array Array of index and labels |
|
| 1397 | - * @param mixed $default the default value |
|
| 1398 | - * @param bool $withNull |
|
| 1399 | - * @return string |
|
| 1400 | - */ |
|
| 1401 | - private static function htmlSelectOptions($array, $default = 0, $withNull = true) |
|
| 1402 | - { |
|
| 1403 | - $ret = array(); |
|
| 1404 | - $selected = ''; |
|
| 1405 | - if ($withNull) { |
|
| 1406 | - if (!is_array($default)) { |
|
| 1407 | - if ($default === 0) { |
|
| 1408 | - $selected = " selected = 'selected'"; |
|
| 1409 | - } |
|
| 1410 | - } else { |
|
| 1411 | - if (in_array(0, $default)) { |
|
| 1412 | - $selected = " selected = 'selected'"; |
|
| 1413 | - } |
|
| 1414 | - } |
|
| 1415 | - $ret[] = '<option value=0' . $selected . '>---</option>'; |
|
| 1416 | - } |
|
| 1417 | - |
|
| 1418 | - foreach ($array as $index => $label) { |
|
| 1419 | - $selected = ''; |
|
| 1420 | - if (!is_array($default)) { |
|
| 1421 | - if ($index == $default) { |
|
| 1422 | - $selected = " selected = 'selected'"; |
|
| 1423 | - } |
|
| 1424 | - } else { |
|
| 1425 | - if (in_array($index, $default)) { |
|
| 1426 | - $selected = " selected = 'selected'"; |
|
| 1427 | - } |
|
| 1428 | - } |
|
| 1429 | - $ret[] = "<option value=\"" . $index . "\"" . $selected . '>' . $label . '</option>'; |
|
| 1430 | - } |
|
| 1431 | - |
|
| 1432 | - return implode("\n", $ret); |
|
| 1433 | - } |
|
| 1434 | - |
|
| 1435 | - /** |
|
| 1436 | - * Creates an html select an returns its code |
|
| 1437 | - * |
|
| 1438 | - * @param string $selectName Select's name |
|
| 1439 | - * @param array $array Key = value of each option, value = label of each option |
|
| 1440 | - * @param mixed $default Default's value |
|
| 1441 | - * @param boolean $withNull Do we want a null option |
|
| 1442 | - * @param string $style Is there a style to apply ? |
|
| 1443 | - * @param boolean $multiple Can we select multiple elements ? |
|
| 1444 | - * @param integer $size Size of the selectbox, only used with multiple selectboxes |
|
| 1445 | - * @param string $extra |
|
| 1446 | - * @return string |
|
| 1447 | - */ |
|
| 1448 | - public static function htmlSelect($selectName, $array, $default, $withNull = true, $style = '', $multiple = false, $size = 1, $extra = '') |
|
| 1449 | - { |
|
| 1450 | - $ret = ''; |
|
| 1451 | - $ret .= "<select name='" . $selectName . "' id='" . $selectName . "'"; |
|
| 1452 | - if (xoops_trim($style) != '') { |
|
| 1453 | - $ret .= " style='" . $style . "' "; |
|
| 1454 | - } |
|
| 1455 | - if (xoops_trim($multiple) != '') { |
|
| 1456 | - $ret .= " multiple = 'multiple' size='" . $size . "' "; |
|
| 1457 | - } |
|
| 1458 | - $ret .= $extra . ">\n"; |
|
| 1459 | - $ret .= self::htmlSelectOptions($array, $default, $withNull); |
|
| 1460 | - $ret .= "</select>\n"; |
|
| 1461 | - |
|
| 1462 | - return $ret; |
|
| 1463 | - } |
|
| 1464 | - |
|
| 1465 | - /** |
|
| 1466 | - * Verify that a mysql table exists |
|
| 1467 | - * @param $tablename |
|
| 1468 | - * @return bool |
|
| 1469 | - */ |
|
| 1470 | - public static function tableExists($tablename) |
|
| 1471 | - { |
|
| 1472 | - global $xoopsDB; |
|
| 1473 | - $result = $xoopsDB->queryF("SHOW TABLES LIKE '$tablename'"); |
|
| 1474 | - |
|
| 1475 | - return ($xoopsDB->getRowsNum($result) > 0); |
|
| 1476 | - } |
|
| 1477 | - |
|
| 1478 | - /** |
|
| 1479 | - * Verify that a field exists inside a mysql table |
|
| 1480 | - * @param $fieldname |
|
| 1481 | - * @param $table |
|
| 1482 | - * @return bool |
|
| 1483 | - */ |
|
| 1484 | - public static function fieldExists($fieldname, $table) |
|
| 1485 | - { |
|
| 1486 | - global $xoopsDB; |
|
| 1487 | - $result = $xoopsDB->queryF("SHOW COLUMNS FROM $table LIKE '$fieldname'"); |
|
| 1488 | - |
|
| 1489 | - return ($xoopsDB->getRowsNum($result) > 0); |
|
| 1490 | - } |
|
| 1491 | - |
|
| 1492 | - /** |
|
| 1493 | - * Retourne la définition d'un champ |
|
| 1494 | - * |
|
| 1495 | - * @param string $fieldname |
|
| 1496 | - * @param string $table |
|
| 1497 | - * @return array |
|
| 1498 | - */ |
|
| 1499 | - public static function getFieldDefinition($fieldname, $table) |
|
| 1500 | - { |
|
| 1501 | - global $xoopsDB; |
|
| 1502 | - $result = $xoopsDB->queryF("SHOW COLUMNS FROM $table LIKE '$fieldname'"); |
|
| 1503 | - if ($result) { |
|
| 1504 | - return $xoopsDB->fetchArray($result); |
|
| 1505 | - } |
|
| 1506 | - |
|
| 1507 | - return ''; |
|
| 1508 | - } |
|
| 1509 | - |
|
| 1510 | - /** |
|
| 1511 | - * Add a field to a mysql table |
|
| 1512 | - * @param $field |
|
| 1513 | - * @param $table |
|
| 1514 | - * @return resource |
|
| 1515 | - */ |
|
| 1516 | - public static function addField($field, $table) |
|
| 1517 | - { |
|
| 1518 | - global $xoopsDB; |
|
| 1519 | - $result = $xoopsDB->queryF("ALTER TABLE $table ADD $field;"); |
|
| 1520 | - |
|
| 1521 | - return $result; |
|
| 1522 | - } |
|
| 1523 | - |
|
| 1524 | - /** |
|
| 1525 | - * Maintenance des tables du module et de son cache de requêtes |
|
| 1526 | - * |
|
| 1527 | - * @return void |
|
| 1528 | - */ |
|
| 1529 | - public static function maintainTablesCache() |
|
| 1530 | - { |
|
| 1531 | - global $xoopsDB; |
|
| 1532 | - define('REFERENCES_MAINTAIN', true); |
|
| 1533 | - require self::MODULE_PATH . 'xoops_version.php'; |
|
| 1534 | - $tables = array(); |
|
| 1535 | - foreach ($modversion['tables'] as $table) { |
|
| 1536 | - $tables[] = $xoopsDB->prefix($table); |
|
| 1537 | - } |
|
| 1538 | - if (count($tables) > 0) { |
|
| 1539 | - $list = implode(',', $tables); |
|
| 1540 | - $xoopsDB->queryF('CHECK TABLE ' . $list); |
|
| 1541 | - $xoopsDB->queryF('ANALYZE TABLE ' . $list); |
|
| 1542 | - $xoopsDB->queryF('OPTIMIZE TABLE ' . $list); |
|
| 1543 | - } |
|
| 1544 | - self::updateCache(); |
|
| 1545 | - $handlers = references_handler::getInstance(); |
|
| 1546 | - $handlers->h_references_articles->forceCacheClean(); |
|
| 1547 | - } |
|
| 1548 | - |
|
| 1549 | - /** |
|
| 1550 | - * Indique si le module Tag existe et est activé |
|
| 1551 | - * |
|
| 1552 | - * @return boolean |
|
| 1553 | - * @credit: smartfactory |
|
| 1554 | - */ |
|
| 1555 | - public static function tagModuleExists() |
|
| 1556 | - { |
|
| 1557 | - static $moduleExistsAndIsActive; |
|
| 1558 | - if (!isset($moduleExistsAndIsActive)) { |
|
| 1559 | - $modules_handler = xoops_getHandler('module'); |
|
| 1560 | - $tagModule = $modules_handler->getByDirName('tag'); |
|
| 1561 | - if (!$tagModule) { |
|
| 1562 | - $moduleExistsAndIsActive = false; |
|
| 1563 | - } else { |
|
| 1564 | - $moduleExistsAndIsActive = $tagModule->getVar('isactive') == 1; |
|
| 1565 | - } |
|
| 1566 | - } |
|
| 1567 | - |
|
| 1568 | - return $moduleExistsAndIsActive; |
|
| 1569 | - } |
|
| 809 | + $chaine = str_replace($search, $replace, $chaine); |
|
| 810 | + |
|
| 811 | + return $chaine; |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + /** |
|
| 815 | + * Création d'une titre pour être utilisé par l'url rewriting |
|
| 816 | + * |
|
| 817 | + * @param string $content Le texte à utiliser pour créer l'url |
|
| 818 | + * @param integer $urw La limite basse pour créer les mots |
|
| 819 | + * @return string Le texte à utiliser pour l'url |
|
| 820 | + * Note, some parts are from Solo's code |
|
| 821 | + */ |
|
| 822 | + public static function makeSeoUrl($content, $urw = 1) |
|
| 823 | + { |
|
| 824 | + $s = "ÀÁÂÃÄÅÒÓÔÕÖØÈÉÊËÇÌÍÎÏÙÚÛÜŸÑàáâãäåòóôõöøèéêëçìíîïùúûüÿñ '()"; |
|
| 825 | + $r = 'AAAAAAOOOOOOEEEECIIIIUUUUYNaaaaaaooooooeeeeciiiiuuuuyn----'; |
|
| 826 | + $content = self::unhtml($content); // First, remove html entities |
|
| 827 | + $content = strtr($content, $s, $r); |
|
| 828 | + $content = strip_tags($content); |
|
| 829 | + $content = strtolower($content); |
|
| 830 | + $content = htmlentities($content); // TODO: Vérifier |
|
| 831 | + $content = preg_replace('/&([a-zA-Z])(uml|acute|grave|circ|tilde);/', '$1', $content); |
|
| 832 | + $content = html_entity_decode($content); |
|
| 833 | + $content = eregi_replace('quot', ' ', $content); |
|
| 834 | + $content = eregi_replace("'", ' ', $content); |
|
| 835 | + $content = eregi_replace('-', ' ', $content); |
|
| 836 | + $content = eregi_replace('[[:punct:]]', '', $content); |
|
| 837 | + // Selon option mais attention au fichier .htaccess ! |
|
| 838 | + // $content = eregi_replace('[[:digit:]]','', $content); |
|
| 839 | + $content = preg_replace('/[^a-z|A-Z|0-9]/', '-', $content); |
|
| 840 | + |
|
| 841 | + $words = explode(' ', $content); |
|
| 842 | + $keywords = ''; |
|
| 843 | + foreach ($words as $word) { |
|
| 844 | + if (strlen($word) >= $urw) { |
|
| 845 | + $keywords .= '-' . trim($word); |
|
| 846 | + } |
|
| 847 | + } |
|
| 848 | + if (!$keywords) { |
|
| 849 | + $keywords = '-'; |
|
| 850 | + } |
|
| 851 | + // Supprime les tirets en double |
|
| 852 | + $keywords = str_replace('---', '-', $keywords); |
|
| 853 | + $keywords = str_replace('--', '-', $keywords); |
|
| 854 | + // Supprime un éventuel tiret à la fin de la chaine |
|
| 855 | + if (substr($keywords, strlen($keywords) - 1, 1) == '-') { |
|
| 856 | + $keywords = substr($keywords, 0, strlen($keywords) - 1); |
|
| 857 | + } |
|
| 858 | + |
|
| 859 | + return $keywords; |
|
| 860 | + } |
|
| 861 | + |
|
| 862 | + /** |
|
| 863 | + * Create the meta keywords based on the content |
|
| 864 | + * |
|
| 865 | + * @param string $content Content from which we have to create metakeywords |
|
| 866 | + * @return string The list of meta keywords |
|
| 867 | + */ |
|
| 868 | + public static function createMetaKeywords($content) |
|
| 869 | + { |
|
| 870 | + $keywordscount = REFERENCES_METAGEN_MAX_KEYWORDS; |
|
| 871 | + $keywordsorder = REFERENCES_METAGEN_KEYWORDS_ORDER; |
|
| 872 | + |
|
| 873 | + $tmp = array(); |
|
| 874 | + // Search for the "Minimum keyword length" |
|
| 875 | + // TODO: Remplacer references_keywords_limit par une constante |
|
| 876 | + if (isset($_SESSION['references_keywords_limit'])) { |
|
| 877 | + $limit = $_SESSION['references_keywords_limit']; |
|
| 878 | + } else { |
|
| 879 | + $config_handler = xoops_getHandler('config'); |
|
| 880 | + $xoopsConfigSearch =& $config_handler->getConfigsByCat(XOOPS_CONF_SEARCH); |
|
| 881 | + $limit = $xoopsConfigSearch['keyword_min']; |
|
| 882 | + $_SESSION['references_keywords_limit'] = $limit; |
|
| 883 | + } |
|
| 884 | + $myts = MyTextSanitizer::getInstance(); |
|
| 885 | + $content = str_replace('<br>', ' ', $content); |
|
| 886 | + $content = $myts->undoHtmlSpecialChars($content); |
|
| 887 | + $content = strip_tags($content); |
|
| 888 | + $content = strtolower($content); |
|
| 889 | + $search_pattern = array(' ', "\t", "\r\n", "\r", "\n", ',', '.', "'", ';', ':', ')', '(', '"', '?', '!', '{', '}', '[', ']', '<', '>', '/', '+', '-', '_', '\\', '*'); |
|
| 890 | + $replace_pattern = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); |
|
| 891 | + $content = str_replace($search_pattern, $replace_pattern, $content); |
|
| 892 | + $keywords = explode(' ', $content); |
|
| 893 | + switch ($keywordsorder) { |
|
| 894 | + case 0: // Ordre d'apparition dans le texte |
|
| 895 | + $keywords = array_unique($keywords); |
|
| 896 | + break; |
|
| 897 | + case 1: // Ordre de fréquence des mots |
|
| 898 | + $keywords = array_count_values($keywords); |
|
| 899 | + asort($keywords); |
|
| 900 | + $keywords = array_keys($keywords); |
|
| 901 | + break; |
|
| 902 | + case 2: // Ordre inverse de la fréquence des mots |
|
| 903 | + $keywords = array_count_values($keywords); |
|
| 904 | + arsort($keywords); |
|
| 905 | + $keywords = array_keys($keywords); |
|
| 906 | + break; |
|
| 907 | + } |
|
| 908 | + // Remove black listed words |
|
| 909 | + if (xoops_trim(self::getModuleOption('metagen_blacklist')) != '') { |
|
| 910 | + $metagen_blacklist = str_replace("\r", '', self::getModuleOption('metagen_blacklist')); |
|
| 911 | + $metablack = explode("\n", $metagen_blacklist); |
|
| 912 | + array_walk($metablack, 'trim'); |
|
| 913 | + $keywords = array_diff($keywords, $metablack); |
|
| 914 | + } |
|
| 915 | + |
|
| 916 | + foreach ($keywords as $keyword) { |
|
| 917 | + if (strlen($keyword) >= $limit && !is_numeric($keyword)) { |
|
| 918 | + $tmp[] = $keyword; |
|
| 919 | + } |
|
| 920 | + } |
|
| 921 | + $tmp = array_slice($tmp, 0, $keywordscount); |
|
| 922 | + if (count($tmp) > 0) { |
|
| 923 | + return implode(',', $tmp); |
|
| 924 | + } else { |
|
| 925 | + if (!isset($config_handler) || !is_object($config_handler)) { |
|
| 926 | + $config_handler = xoops_getHandler('config'); |
|
| 927 | + } |
|
| 928 | + $xoopsConfigMetaFooter =& $config_handler->getConfigsByCat(XOOPS_CONF_METAFOOTER); |
|
| 929 | + if (isset($xoopsConfigMetaFooter['meta_keywords'])) { |
|
| 930 | + return $xoopsConfigMetaFooter['meta_keywords']; |
|
| 931 | + } else { |
|
| 932 | + return ''; |
|
| 933 | + } |
|
| 934 | + } |
|
| 935 | + } |
|
| 936 | + |
|
| 937 | + /** |
|
| 938 | + * Fonction chargée de gérer l'upload |
|
| 939 | + * |
|
| 940 | + * @param integer $indice L'indice du fichier à télécharger |
|
| 941 | + * @param string $dstpath |
|
| 942 | + * @param null $mimeTypes |
|
| 943 | + * @param null $uploadMaxSize |
|
| 944 | + * @param null $maxWidth |
|
| 945 | + * @param null $maxHeight |
|
| 946 | + * @return mixed True si l'upload s'est bien déroulé sinon le message d'erreur correspondant |
|
| 947 | + */ |
|
| 948 | + public static function uploadFile($indice, $dstpath = XOOPS_UPLOAD_PATH, $mimeTypes = null, $uploadMaxSize = null, $maxWidth = null, $maxHeight = null) |
|
| 949 | + { |
|
| 950 | + require_once XOOPS_ROOT_PATH . '/class/uploader.php'; |
|
| 951 | + global $destname; |
|
| 952 | + if (isset($_POST['xoops_upload_file'])) { |
|
| 953 | + require_once XOOPS_ROOT_PATH . '/class/uploader.php'; |
|
| 954 | + $fldname = ''; |
|
| 955 | + $fldname = $_FILES[$_POST['xoops_upload_file'][$indice]]; |
|
| 956 | + $fldname = get_magic_quotes_gpc() ? stripslashes($fldname['name']) : $fldname['name']; |
|
| 957 | + if (xoops_trim($fldname != '')) { |
|
| 958 | + $destname = self::createUploadName($dstpath, $fldname, true); |
|
| 959 | + if ($mimeTypes === null) { |
|
| 960 | + $permittedtypes = explode("\n", str_replace("\r", '', self::getModuleOption('mimetypes'))); |
|
| 961 | + array_walk($permittedtypes, 'trim'); |
|
| 962 | + } else { |
|
| 963 | + $permittedtypes = $mimeTypes; |
|
| 964 | + } |
|
| 965 | + if ($uploadMaxSize === null) { |
|
| 966 | + $uploadSize = self::getModuleOption('maxuploadsize'); |
|
| 967 | + } else { |
|
| 968 | + $uploadSize = $uploadMaxSize; |
|
| 969 | + } |
|
| 970 | + $uploader = new XoopsMediaUploader($dstpath, $permittedtypes, $uploadSize, $maxWidth, $maxHeight); |
|
| 971 | + //$uploader->allowUnknownTypes = true; |
|
| 972 | + $uploader->setTargetFileName($destname); |
|
| 973 | + if ($uploader->fetchMedia($_POST['xoops_upload_file'][$indice])) { |
|
| 974 | + if ($uploader->upload()) { |
|
| 975 | + return true; |
|
| 976 | + } else { |
|
| 977 | + return _ERRORS . ' ' . htmlentities($uploader->getErrors()); |
|
| 978 | + } |
|
| 979 | + } else { |
|
| 980 | + return htmlentities($uploader->getErrors()); |
|
| 981 | + } |
|
| 982 | + } else { |
|
| 983 | + return false; |
|
| 984 | + } |
|
| 985 | + } else { |
|
| 986 | + return false; |
|
| 987 | + } |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + /** |
|
| 991 | + * Resize a Picture to some given dimensions (using the wideImage library) |
|
| 992 | + * |
|
| 993 | + * @param string $src_path Picture's source |
|
| 994 | + * @param string $dst_path Picture's destination |
|
| 995 | + * @param integer $param_width Maximum picture's width |
|
| 996 | + * @param integer $param_height Maximum picture's height |
|
| 997 | + * @param boolean $keep_original Do we have to keep the original picture ? |
|
| 998 | + * @param string $fit Resize mode (see the wideImage library for more information) |
|
| 999 | + * @return bool |
|
| 1000 | + */ |
|
| 1001 | + public static function resizePicture($src_path, $dst_path, $param_width, $param_height, $keep_original = false, $fit = 'inside') |
|
| 1002 | + { |
|
| 1003 | + // require_once self::MODULE_PATH . 'class/wideimage/WideImage.php'; |
|
| 1004 | + $img = WideImage::load($src_path); |
|
| 1005 | + $result = $img->resize($param_width, $param_height, $fit); |
|
| 1006 | + $result->saveToFile($dst_path, null); |
|
| 1007 | + if (!$keep_original) { |
|
| 1008 | + @unlink($src_path); |
|
| 1009 | + } |
|
| 1010 | + |
|
| 1011 | + return true; |
|
| 1012 | + } |
|
| 1013 | + |
|
| 1014 | + /** |
|
| 1015 | + * Déclenchement d'une alerte Xoops suite à un évènement |
|
| 1016 | + * |
|
| 1017 | + * @param string $category La catégorie de l'évènement |
|
| 1018 | + * @param integer $itemId L'ID de l'élément (trop général pour être décris précisément) |
|
| 1019 | + * @param unknown_type $event L'évènement qui est déclencé |
|
| 1020 | + * @param unknown_type $tags Les variables à passer au template |
|
| 1021 | + */ |
|
| 1022 | + public static function notify($category, $itemId, $event, $tags) |
|
| 1023 | + { |
|
| 1024 | + $notification_handler = xoops_getHandler('notification'); |
|
| 1025 | + $tags['X_MODULE_URL'] = REFERENCES_URL; |
|
| 1026 | + $notification_handler->triggerEvent($category, $itemId, $event, $tags); |
|
| 1027 | + } |
|
| 1028 | + |
|
| 1029 | + /** |
|
| 1030 | + * Ajoute des jours à une date et retourne la nouvelle date au format Date de Mysql |
|
| 1031 | + * |
|
| 1032 | + * @param int $duration |
|
| 1033 | + * @param integer $startingDate Date de départ (timestamp) |
|
| 1034 | + * @return bool|string |
|
| 1035 | + * @internal param int $durations Durée en jours |
|
| 1036 | + */ |
|
| 1037 | + public static function addDaysToDate($duration = 1, $startingDate = 0) |
|
| 1038 | + { |
|
| 1039 | + if ($startingDate == 0) { |
|
| 1040 | + $startingDate = time(); |
|
| 1041 | + } |
|
| 1042 | + $endingDate = $startingDate + ($duration * 86400); |
|
| 1043 | + |
|
| 1044 | + return date('Y-m-d', $endingDate); |
|
| 1045 | + } |
|
| 1046 | + |
|
| 1047 | + /** |
|
| 1048 | + * Retourne un breadcrumb en fonction des paramètres passés et en partant (d'office) de la racine du module |
|
| 1049 | + * |
|
| 1050 | + * @param array $path Le chemin complet (excepté la racine) du breadcrumb sous la forme clé=url valeur=titre |
|
| 1051 | + * @param string $raquo Le séparateur par défaut à utiliser |
|
| 1052 | + * @return string le breadcrumb |
|
| 1053 | + */ |
|
| 1054 | + public static function breadcrumb($path, $raquo = ' » ') |
|
| 1055 | + { |
|
| 1056 | + $breadcrumb = ''; |
|
| 1057 | + $workingBreadcrumb = array(); |
|
| 1058 | + if (is_array($path)) { |
|
| 1059 | + $moduleName = self::getModuleName(); |
|
| 1060 | + $workingBreadcrumb[] = "<a href='" . REFERENCES_URL . "' title='" . self::makeHrefTitle($moduleName) . "'>" . $moduleName . '</a>'; |
|
| 1061 | + foreach ($path as $url => $title) { |
|
| 1062 | + $workingBreadcrumb[] = "<a href='" . $url . "'>" . $title . '</a>'; |
|
| 1063 | + } |
|
| 1064 | + $cnt = count($workingBreadcrumb); |
|
| 1065 | + for ($i = 0; $i < $cnt; ++$i) { |
|
| 1066 | + if ($i == $cnt - 1) { |
|
| 1067 | + $workingBreadcrumb[$i] = strip_tags($workingBreadcrumb[$i]); |
|
| 1068 | + } |
|
| 1069 | + } |
|
| 1070 | + $breadcrumb = implode($raquo, $workingBreadcrumb); |
|
| 1071 | + } |
|
| 1072 | + |
|
| 1073 | + return $breadcrumb; |
|
| 1074 | + } |
|
| 1075 | + |
|
| 1076 | + public static function close_tags($string) |
|
| 1077 | + { |
|
| 1078 | + // match opened tags |
|
| 1079 | + if (preg_match_all('/<([a-z\:\-]+)[^\/]>/', $string, $start_tags)) { |
|
| 1080 | + $start_tags = $start_tags[1]; |
|
| 1081 | + |
|
| 1082 | + // match closed tags |
|
| 1083 | + if (preg_match_all('/<\/([a-z]+)>/', $string, $end_tags)) { |
|
| 1084 | + $complete_tags = array(); |
|
| 1085 | + $end_tags = $end_tags[1]; |
|
| 1086 | + |
|
| 1087 | + foreach ($start_tags as $key => $val) { |
|
| 1088 | + $posb = array_search($val, $end_tags); |
|
| 1089 | + if (is_int($posb)) { |
|
| 1090 | + unset($end_tags[$posb]); |
|
| 1091 | + } else { |
|
| 1092 | + $complete_tags[] = $val; |
|
| 1093 | + } |
|
| 1094 | + } |
|
| 1095 | + } else { |
|
| 1096 | + $complete_tags = $start_tags; |
|
| 1097 | + } |
|
| 1098 | + |
|
| 1099 | + $complete_tags = array_reverse($complete_tags); |
|
| 1100 | + for ($i = 0; $i < count($complete_tags); ++$i) { |
|
| 1101 | + $string .= '</' . $complete_tags[$i] . '>'; |
|
| 1102 | + } |
|
| 1103 | + } |
|
| 1104 | + |
|
| 1105 | + return $string; |
|
| 1106 | + } |
|
| 1107 | + |
|
| 1108 | + public static function truncate_tagsafe($string, $length = 80, $etc = '...', $break_words = false) |
|
| 1109 | + { |
|
| 1110 | + if ($length == 0) { |
|
| 1111 | + return ''; |
|
| 1112 | + } |
|
| 1113 | + |
|
| 1114 | + if (strlen($string) > $length) { |
|
| 1115 | + $length -= strlen($etc); |
|
| 1116 | + if (!$break_words) { |
|
| 1117 | + $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1)); |
|
| 1118 | + $string = preg_replace('/<[^>]*$/', '', $string); |
|
| 1119 | + $string = self::close_tags($string); |
|
| 1120 | + } |
|
| 1121 | + |
|
| 1122 | + return $string . $etc; |
|
| 1123 | + } else { |
|
| 1124 | + return $string; |
|
| 1125 | + } |
|
| 1126 | + } |
|
| 1127 | + |
|
| 1128 | + /** |
|
| 1129 | + * Create an infotip |
|
| 1130 | + * @param $text |
|
| 1131 | + * @return string |
|
| 1132 | + */ |
|
| 1133 | + public static function makeInfotips($text) |
|
| 1134 | + { |
|
| 1135 | + $ret = ''; |
|
| 1136 | + $infotips = self::getModuleOption('infotips'); |
|
| 1137 | + if ($infotips > 0) { |
|
| 1138 | + $myts = MyTextSanitizer::getInstance(); |
|
| 1139 | + $ret = $myts->htmlSpecialChars(substr(strip_tags($text), 0, $infotips)); |
|
| 1140 | + } |
|
| 1141 | + |
|
| 1142 | + return $ret; |
|
| 1143 | + } |
|
| 1144 | + |
|
| 1145 | + /** |
|
| 1146 | + * Retourne le type Mime d'un fichier en utilisant d'abord finfo puis mime_content |
|
| 1147 | + * |
|
| 1148 | + * @param string $filename Le fichier (avec son chemin d'accès complet) dont on veut connaître le type mime |
|
| 1149 | + * @return string |
|
| 1150 | + */ |
|
| 1151 | + public static function getMimeType($filename) |
|
| 1152 | + { |
|
| 1153 | + if (function_exists('finfo_open')) { |
|
| 1154 | + $pathToMagic = REFERENCES_PATH . 'mime/magic'; |
|
| 1155 | + $finfo = new finfo(FILEINFO_MIME, $pathToMagic); |
|
| 1156 | + $mimetype = $finfo->file($filename); |
|
| 1157 | + finfo_close($finfo); |
|
| 1158 | + unset($finfo); |
|
| 1159 | + |
|
| 1160 | + return $mimetype; |
|
| 1161 | + } else { |
|
| 1162 | + if (function_exists('mime_content_type')) { |
|
| 1163 | + return mime_content_type($filename); |
|
| 1164 | + } else { |
|
| 1165 | + return ''; |
|
| 1166 | + } |
|
| 1167 | + } |
|
| 1168 | + } |
|
| 1169 | + |
|
| 1170 | + /** |
|
| 1171 | + * Retourne une liste d'objets XoopsUsers à partir d'une liste d'identifiants |
|
| 1172 | + * |
|
| 1173 | + * @param array $xoopsUsersIDs La liste des ID |
|
| 1174 | + * @return array Les objets XoopsUsers |
|
| 1175 | + */ |
|
| 1176 | + public static function getUsersFromIds($xoopsUsersIDs) |
|
| 1177 | + { |
|
| 1178 | + $users = array(); |
|
| 1179 | + if (is_array($xoopsUsersIDs) && count($xoopsUsersIDs) > 0) { |
|
| 1180 | + $xoopsUsersIDs = array_unique($xoopsUsersIDs); |
|
| 1181 | + sort($xoopsUsersIDs); |
|
| 1182 | + if (count($xoopsUsersIDs) > 0) { |
|
| 1183 | + $member_handler = xoops_getHandler('user'); |
|
| 1184 | + $criteria = new Criteria('uid', '(' . implode(',', $xoopsUsersIDs) . ')', 'IN'); |
|
| 1185 | + $criteria->setSort('uid'); |
|
| 1186 | + $users = $member_handler->getObjects($criteria, true); |
|
| 1187 | + } |
|
| 1188 | + } |
|
| 1189 | + |
|
| 1190 | + return $users; |
|
| 1191 | + } |
|
| 1192 | + |
|
| 1193 | + /** |
|
| 1194 | + * Retourne l'ID de l'utilisateur courant (s'il est connecté) |
|
| 1195 | + * @return integer L'uid ou 0 |
|
| 1196 | + */ |
|
| 1197 | + public static function getCurrentUserID() |
|
| 1198 | + { |
|
| 1199 | + global $xoopsUser; |
|
| 1200 | + $uid = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0; |
|
| 1201 | + |
|
| 1202 | + return $uid; |
|
| 1203 | + } |
|
| 1204 | + |
|
| 1205 | + /** |
|
| 1206 | + * Retourne la liste des groupes de l'utilisateur courant (avec cache) |
|
| 1207 | + * @param int $uid |
|
| 1208 | + * @return array Les ID des groupes auquel l'utilisateur courant appartient |
|
| 1209 | + */ |
|
| 1210 | + public static function getMemberGroups($uid = 0) |
|
| 1211 | + { |
|
| 1212 | + static $buffer = array(); |
|
| 1213 | + if ($uid == 0) { |
|
| 1214 | + $uid = self::getCurrentUserID(); |
|
| 1215 | + } |
|
| 1216 | + |
|
| 1217 | + if (is_array($buffer) && count($buffer) > 0 && isset($buffer[$uid])) { |
|
| 1218 | + return $buffer[$uid]; |
|
| 1219 | + } else { |
|
| 1220 | + if ($uid > 0) { |
|
| 1221 | + $member_handler = xoops_getHandler('member'); |
|
| 1222 | + $buffer[$uid] = $member_handler->getGroupsByUser($uid, false); // Renvoie un tableau d'ID (de groupes) |
|
| 1223 | + } else { |
|
| 1224 | + $buffer[$uid] = array(XOOPS_GROUP_ANONYMOUS); |
|
| 1225 | + } |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + return $buffer[$uid]; |
|
| 1229 | + } |
|
| 1230 | + |
|
| 1231 | + /** |
|
| 1232 | + * Indique si l'utilisateur courant fait partie d'une groupe donné (avec gestion de cache) |
|
| 1233 | + * |
|
| 1234 | + * @param integer $group Groupe recherché |
|
| 1235 | + * @param int $uid |
|
| 1236 | + * @return bool vrai si l'utilisateur fait partie du groupe, faux sinon |
|
| 1237 | + */ |
|
| 1238 | + public static function isMemberOfGroup($group = 0, $uid = 0) |
|
| 1239 | + { |
|
| 1240 | + static $buffer = array(); |
|
| 1241 | + $retval = false; |
|
| 1242 | + if ($uid == 0) { |
|
| 1243 | + $uid = self::getCurrentUserID(); |
|
| 1244 | + } |
|
| 1245 | + if (is_array($buffer) && array_key_exists($group, $buffer)) { |
|
| 1246 | + $retval = $buffer[$group]; |
|
| 1247 | + } else { |
|
| 1248 | + $member_handler = xoops_getHandler('member'); |
|
| 1249 | + $groups = $member_handler->getGroupsByUser($uid, false); // Renvoie un tableau d'ID (de groupes) |
|
| 1250 | + $retval = in_array($group, $groups); |
|
| 1251 | + $buffer[$group] = $retval; |
|
| 1252 | + } |
|
| 1253 | + |
|
| 1254 | + return $retval; |
|
| 1255 | + } |
|
| 1256 | + |
|
| 1257 | + /** |
|
| 1258 | + * Indique si un utilisateur fait partie de plusieurs groupes |
|
| 1259 | + * La fonction renvoie vrai dès qu'on trouve que le membre fait partir d'un des groupes |
|
| 1260 | + * @param array $groups La liste des groupes à vérifier |
|
| 1261 | + * @param integer $uid L'ID de l'utilisateur |
|
| 1262 | + * @return boolean |
|
| 1263 | + */ |
|
| 1264 | + public static function isMemberOfGroups($groups, $uid = 0) |
|
| 1265 | + { |
|
| 1266 | + if (count($groups) == 0) { |
|
| 1267 | + return false; |
|
| 1268 | + } |
|
| 1269 | + if ($uid == 0) { |
|
| 1270 | + $uid = self::getCurrentUserID(); |
|
| 1271 | + } |
|
| 1272 | + foreach ($groups as $groupId) { |
|
| 1273 | + if (self::isMemberOfGroup($groupId, $uid)) { |
|
| 1274 | + return true; |
|
| 1275 | + } |
|
| 1276 | + } |
|
| 1277 | + |
|
| 1278 | + return false; |
|
| 1279 | + } |
|
| 1280 | + |
|
| 1281 | + /** |
|
| 1282 | + * Retourne le nombre total de groupes (y compris les anonymes) |
|
| 1283 | + */ |
|
| 1284 | + public static function getGroupsCount() |
|
| 1285 | + { |
|
| 1286 | + static $cache = -1; |
|
| 1287 | + if ($cache == -1) { |
|
| 1288 | + global $xoopsDB; |
|
| 1289 | + $sql = 'SELECT COUNT(*) FROM ' . $xoopsDB->prefix('groups'); |
|
| 1290 | + $result = $xoopsDB->query($sql); |
|
| 1291 | + if (!$result) { |
|
| 1292 | + return false; |
|
| 1293 | + } |
|
| 1294 | + list($cache) = $xoopsDB->fetchRow($result); |
|
| 1295 | + } |
|
| 1296 | + |
|
| 1297 | + return $cache; |
|
| 1298 | + } |
|
| 1299 | + |
|
| 1300 | + /** |
|
| 1301 | + * Fonction chargée de vérifier qu'un répertoire existe, qu'on peut écrire dedans et création d'un fichier index.html |
|
| 1302 | + * |
|
| 1303 | + * @param string $folder Le chemin complet du répertoire à vérifier |
|
| 1304 | + * @return void |
|
| 1305 | + */ |
|
| 1306 | + public static function prepareFolder($folder) |
|
| 1307 | + { |
|
| 1308 | + if (!is_dir($folder)) { |
|
| 1309 | + mkdir($folder, 0777); |
|
| 1310 | + file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
|
| 1311 | + } else { |
|
| 1312 | + if (!is_writable($folder)) { |
|
| 1313 | + chmod($folder, 0777); |
|
| 1314 | + } |
|
| 1315 | + } |
|
| 1316 | + } |
|
| 1317 | + |
|
| 1318 | + /** |
|
| 1319 | + * Load a language file |
|
| 1320 | + * |
|
| 1321 | + * @param string $languageFile The required language file |
|
| 1322 | + * @param string $defaultExtension Default extension to use |
|
| 1323 | + */ |
|
| 1324 | + public static function loadLanguageFile($languageFile, $defaultExtension = '.php') |
|
| 1325 | + { |
|
| 1326 | + global $xoopsConfig; |
|
| 1327 | + $root = self::MODULE_PATH; |
|
| 1328 | + if (strstr($languageFile, $defaultExtension) === false) { |
|
| 1329 | + $languageFile .= $defaultExtension; |
|
| 1330 | + } |
|
| 1331 | + if (file_exists($root . 'language' . DIRECTORY_SEPARATOR . $xoopsConfig['language'] . DIRECTORY_SEPARATOR . $languageFile)) { |
|
| 1332 | + require_once $root . 'language' . DIRECTORY_SEPARATOR . $xoopsConfig['language'] . DIRECTORY_SEPARATOR . $languageFile; |
|
| 1333 | + } else { // Fallback |
|
| 1334 | + require_once $root . 'language' . DIRECTORY_SEPARATOR . 'english' . DIRECTORY_SEPARATOR . $languageFile; |
|
| 1335 | + } |
|
| 1336 | + } |
|
| 1337 | + |
|
| 1338 | + /** |
|
| 1339 | + * Création d'une vignette en tenant compte des options du module concernant le mode de création des vignettes |
|
| 1340 | + * |
|
| 1341 | + * @param string $src_path |
|
| 1342 | + * @param string $dst_path |
|
| 1343 | + * @param integer $param_width |
|
| 1344 | + * @param integer $param_height |
|
| 1345 | + * @param boolean $keep_original |
|
| 1346 | + * @param string $fit Utilisé uniquement pour la méthode 0 (redimensionnement) |
|
| 1347 | + * @return boolean |
|
| 1348 | + */ |
|
| 1349 | + public static function createThumb($src_path, $dst_path, $param_width, $param_height, $keep_original = false, $fit = 'inside') |
|
| 1350 | + { |
|
| 1351 | + // require_once self::MODULE_PATH . 'class/wideimage/WideImage.php'; |
|
| 1352 | + $resize = true; |
|
| 1353 | + // On commence par vérifier que l'image originale n'est pas plus petite que les dimensions demandées auquel cas il n'y a rien à faire |
|
| 1354 | + $pictureDimensions = getimagesize($src_path); |
|
| 1355 | + if (is_array($pictureDimensions)) { |
|
| 1356 | + $pictureWidth = $pictureDimensions[0]; |
|
| 1357 | + $pictureHeight = $pictureDimensions[1]; |
|
| 1358 | + if ($pictureWidth < $param_width && $pictureHeight < $param_height) { |
|
| 1359 | + $resize = false; |
|
| 1360 | + } |
|
| 1361 | + } |
|
| 1362 | + $img = WideImage::load($src_path); |
|
| 1363 | + if ($resize) { // L'image est suffisament grande pour être réduite |
|
| 1364 | + $thumbMethod = 0; |
|
| 1365 | + if ($thumbMethod == 0) { // Redimensionnement de l'image |
|
| 1366 | + $result = $img->resize($param_width, $param_height, $fit); |
|
| 1367 | + } else { // Récupération d'une partie de l'image depuis le centre |
|
| 1368 | + // Calcul de left et top |
|
| 1369 | + $left = $top = 0; |
|
| 1370 | + if (is_array($pictureDimensions)) { |
|
| 1371 | + if ($pictureWidth > $param_width) { |
|
| 1372 | + $left = (int)(($pictureWidth / 2) - ($param_width / 2)); |
|
| 1373 | + } |
|
| 1374 | + if ($pictureHeight > $param_height) { |
|
| 1375 | + $top = (int)(($pictureHeight / 2) - ($param_height / 2)); |
|
| 1376 | + } |
|
| 1377 | + } |
|
| 1378 | + $result = $img->crop($left, $top, $param_width, $param_height); |
|
| 1379 | + } |
|
| 1380 | + $result->saveToFile($dst_path, null); |
|
| 1381 | + } else { |
|
| 1382 | + if ($src_path != $dst_path) { |
|
| 1383 | + @copy($src_path, $dst_path); |
|
| 1384 | + } |
|
| 1385 | + } |
|
| 1386 | + if (!$keep_original) { |
|
| 1387 | + @unlink($src_path); |
|
| 1388 | + } |
|
| 1389 | + |
|
| 1390 | + return true; |
|
| 1391 | + } |
|
| 1392 | + |
|
| 1393 | + /** |
|
| 1394 | + * Create the <option> of an html select |
|
| 1395 | + * |
|
| 1396 | + * @param array $array Array of index and labels |
|
| 1397 | + * @param mixed $default the default value |
|
| 1398 | + * @param bool $withNull |
|
| 1399 | + * @return string |
|
| 1400 | + */ |
|
| 1401 | + private static function htmlSelectOptions($array, $default = 0, $withNull = true) |
|
| 1402 | + { |
|
| 1403 | + $ret = array(); |
|
| 1404 | + $selected = ''; |
|
| 1405 | + if ($withNull) { |
|
| 1406 | + if (!is_array($default)) { |
|
| 1407 | + if ($default === 0) { |
|
| 1408 | + $selected = " selected = 'selected'"; |
|
| 1409 | + } |
|
| 1410 | + } else { |
|
| 1411 | + if (in_array(0, $default)) { |
|
| 1412 | + $selected = " selected = 'selected'"; |
|
| 1413 | + } |
|
| 1414 | + } |
|
| 1415 | + $ret[] = '<option value=0' . $selected . '>---</option>'; |
|
| 1416 | + } |
|
| 1417 | + |
|
| 1418 | + foreach ($array as $index => $label) { |
|
| 1419 | + $selected = ''; |
|
| 1420 | + if (!is_array($default)) { |
|
| 1421 | + if ($index == $default) { |
|
| 1422 | + $selected = " selected = 'selected'"; |
|
| 1423 | + } |
|
| 1424 | + } else { |
|
| 1425 | + if (in_array($index, $default)) { |
|
| 1426 | + $selected = " selected = 'selected'"; |
|
| 1427 | + } |
|
| 1428 | + } |
|
| 1429 | + $ret[] = "<option value=\"" . $index . "\"" . $selected . '>' . $label . '</option>'; |
|
| 1430 | + } |
|
| 1431 | + |
|
| 1432 | + return implode("\n", $ret); |
|
| 1433 | + } |
|
| 1434 | + |
|
| 1435 | + /** |
|
| 1436 | + * Creates an html select an returns its code |
|
| 1437 | + * |
|
| 1438 | + * @param string $selectName Select's name |
|
| 1439 | + * @param array $array Key = value of each option, value = label of each option |
|
| 1440 | + * @param mixed $default Default's value |
|
| 1441 | + * @param boolean $withNull Do we want a null option |
|
| 1442 | + * @param string $style Is there a style to apply ? |
|
| 1443 | + * @param boolean $multiple Can we select multiple elements ? |
|
| 1444 | + * @param integer $size Size of the selectbox, only used with multiple selectboxes |
|
| 1445 | + * @param string $extra |
|
| 1446 | + * @return string |
|
| 1447 | + */ |
|
| 1448 | + public static function htmlSelect($selectName, $array, $default, $withNull = true, $style = '', $multiple = false, $size = 1, $extra = '') |
|
| 1449 | + { |
|
| 1450 | + $ret = ''; |
|
| 1451 | + $ret .= "<select name='" . $selectName . "' id='" . $selectName . "'"; |
|
| 1452 | + if (xoops_trim($style) != '') { |
|
| 1453 | + $ret .= " style='" . $style . "' "; |
|
| 1454 | + } |
|
| 1455 | + if (xoops_trim($multiple) != '') { |
|
| 1456 | + $ret .= " multiple = 'multiple' size='" . $size . "' "; |
|
| 1457 | + } |
|
| 1458 | + $ret .= $extra . ">\n"; |
|
| 1459 | + $ret .= self::htmlSelectOptions($array, $default, $withNull); |
|
| 1460 | + $ret .= "</select>\n"; |
|
| 1461 | + |
|
| 1462 | + return $ret; |
|
| 1463 | + } |
|
| 1464 | + |
|
| 1465 | + /** |
|
| 1466 | + * Verify that a mysql table exists |
|
| 1467 | + * @param $tablename |
|
| 1468 | + * @return bool |
|
| 1469 | + */ |
|
| 1470 | + public static function tableExists($tablename) |
|
| 1471 | + { |
|
| 1472 | + global $xoopsDB; |
|
| 1473 | + $result = $xoopsDB->queryF("SHOW TABLES LIKE '$tablename'"); |
|
| 1474 | + |
|
| 1475 | + return ($xoopsDB->getRowsNum($result) > 0); |
|
| 1476 | + } |
|
| 1477 | + |
|
| 1478 | + /** |
|
| 1479 | + * Verify that a field exists inside a mysql table |
|
| 1480 | + * @param $fieldname |
|
| 1481 | + * @param $table |
|
| 1482 | + * @return bool |
|
| 1483 | + */ |
|
| 1484 | + public static function fieldExists($fieldname, $table) |
|
| 1485 | + { |
|
| 1486 | + global $xoopsDB; |
|
| 1487 | + $result = $xoopsDB->queryF("SHOW COLUMNS FROM $table LIKE '$fieldname'"); |
|
| 1488 | + |
|
| 1489 | + return ($xoopsDB->getRowsNum($result) > 0); |
|
| 1490 | + } |
|
| 1491 | + |
|
| 1492 | + /** |
|
| 1493 | + * Retourne la définition d'un champ |
|
| 1494 | + * |
|
| 1495 | + * @param string $fieldname |
|
| 1496 | + * @param string $table |
|
| 1497 | + * @return array |
|
| 1498 | + */ |
|
| 1499 | + public static function getFieldDefinition($fieldname, $table) |
|
| 1500 | + { |
|
| 1501 | + global $xoopsDB; |
|
| 1502 | + $result = $xoopsDB->queryF("SHOW COLUMNS FROM $table LIKE '$fieldname'"); |
|
| 1503 | + if ($result) { |
|
| 1504 | + return $xoopsDB->fetchArray($result); |
|
| 1505 | + } |
|
| 1506 | + |
|
| 1507 | + return ''; |
|
| 1508 | + } |
|
| 1509 | + |
|
| 1510 | + /** |
|
| 1511 | + * Add a field to a mysql table |
|
| 1512 | + * @param $field |
|
| 1513 | + * @param $table |
|
| 1514 | + * @return resource |
|
| 1515 | + */ |
|
| 1516 | + public static function addField($field, $table) |
|
| 1517 | + { |
|
| 1518 | + global $xoopsDB; |
|
| 1519 | + $result = $xoopsDB->queryF("ALTER TABLE $table ADD $field;"); |
|
| 1520 | + |
|
| 1521 | + return $result; |
|
| 1522 | + } |
|
| 1523 | + |
|
| 1524 | + /** |
|
| 1525 | + * Maintenance des tables du module et de son cache de requêtes |
|
| 1526 | + * |
|
| 1527 | + * @return void |
|
| 1528 | + */ |
|
| 1529 | + public static function maintainTablesCache() |
|
| 1530 | + { |
|
| 1531 | + global $xoopsDB; |
|
| 1532 | + define('REFERENCES_MAINTAIN', true); |
|
| 1533 | + require self::MODULE_PATH . 'xoops_version.php'; |
|
| 1534 | + $tables = array(); |
|
| 1535 | + foreach ($modversion['tables'] as $table) { |
|
| 1536 | + $tables[] = $xoopsDB->prefix($table); |
|
| 1537 | + } |
|
| 1538 | + if (count($tables) > 0) { |
|
| 1539 | + $list = implode(',', $tables); |
|
| 1540 | + $xoopsDB->queryF('CHECK TABLE ' . $list); |
|
| 1541 | + $xoopsDB->queryF('ANALYZE TABLE ' . $list); |
|
| 1542 | + $xoopsDB->queryF('OPTIMIZE TABLE ' . $list); |
|
| 1543 | + } |
|
| 1544 | + self::updateCache(); |
|
| 1545 | + $handlers = references_handler::getInstance(); |
|
| 1546 | + $handlers->h_references_articles->forceCacheClean(); |
|
| 1547 | + } |
|
| 1548 | + |
|
| 1549 | + /** |
|
| 1550 | + * Indique si le module Tag existe et est activé |
|
| 1551 | + * |
|
| 1552 | + * @return boolean |
|
| 1553 | + * @credit: smartfactory |
|
| 1554 | + */ |
|
| 1555 | + public static function tagModuleExists() |
|
| 1556 | + { |
|
| 1557 | + static $moduleExistsAndIsActive; |
|
| 1558 | + if (!isset($moduleExistsAndIsActive)) { |
|
| 1559 | + $modules_handler = xoops_getHandler('module'); |
|
| 1560 | + $tagModule = $modules_handler->getByDirName('tag'); |
|
| 1561 | + if (!$tagModule) { |
|
| 1562 | + $moduleExistsAndIsActive = false; |
|
| 1563 | + } else { |
|
| 1564 | + $moduleExistsAndIsActive = $tagModule->getVar('isactive') == 1; |
|
| 1565 | + } |
|
| 1566 | + } |
|
| 1567 | + |
|
| 1568 | + return $moduleExistsAndIsActive; |
|
| 1569 | + } |
|
| 1570 | 1570 | } |
@@ -25,266 +25,266 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | class references_plugins |
| 27 | 27 | { |
| 28 | - /** |
|
| 29 | - * Dictionnaire des évènements |
|
| 30 | - */ |
|
| 31 | - const EVENT_ON_REFERENCE_CREATE = 'onReferenceCreate'; |
|
| 32 | - const EVENT_ON_CATEGORY_CREATE = 'onCategoryCreate'; |
|
| 28 | + /** |
|
| 29 | + * Dictionnaire des évènements |
|
| 30 | + */ |
|
| 31 | + const EVENT_ON_REFERENCE_CREATE = 'onReferenceCreate'; |
|
| 32 | + const EVENT_ON_CATEGORY_CREATE = 'onCategoryCreate'; |
|
| 33 | 33 | |
| 34 | - // Pour limiter les dépendances |
|
| 35 | - const MODULE_DIRNAME = REFERENCES_DIRNAME; |
|
| 34 | + // Pour limiter les dépendances |
|
| 35 | + const MODULE_DIRNAME = REFERENCES_DIRNAME; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Types d'évènements |
|
| 39 | - */ |
|
| 40 | - const PLUGIN_ACTION = 0; |
|
| 41 | - const PLUGIN_FILTER = 1; |
|
| 37 | + /** |
|
| 38 | + * Types d'évènements |
|
| 39 | + */ |
|
| 40 | + const PLUGIN_ACTION = 0; |
|
| 41 | + const PLUGIN_FILTER = 1; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Nom du script Php inclut qui contient l'inscription des plugins |
|
| 45 | - */ |
|
| 46 | - const PLUGIN_SCRIPT_NAME = 'plugins.php'; |
|
| 43 | + /** |
|
| 44 | + * Nom du script Php inclut qui contient l'inscription des plugins |
|
| 45 | + */ |
|
| 46 | + const PLUGIN_SCRIPT_NAME = 'plugins.php'; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Dans le fichier Php qui contient l'inscription des plugins, méthode à appeler pour récupérer la liste des plugins |
|
| 50 | - */ |
|
| 51 | - const PLUGIN_DESCRIBE_METHOD = 'registerEvents'; |
|
| 48 | + /** |
|
| 49 | + * Dans le fichier Php qui contient l'inscription des plugins, méthode à appeler pour récupérer la liste des plugins |
|
| 50 | + */ |
|
| 51 | + const PLUGIN_DESCRIBE_METHOD = 'registerEvents'; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Nom de la variable de session qui contient la liste des plugins détachés |
|
| 55 | - */ |
|
| 56 | - const PLUGIN_UNPLUG_SESSION_NAME = 'references_plugins'; |
|
| 53 | + /** |
|
| 54 | + * Nom de la variable de session qui contient la liste des plugins détachés |
|
| 55 | + */ |
|
| 56 | + const PLUGIN_UNPLUG_SESSION_NAME = 'references_plugins'; |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Priorités des plugins |
|
| 60 | - * @var constant |
|
| 61 | - */ |
|
| 62 | - const EVENT_PRIORITY_1 = 1; // Priorité la plus haute |
|
| 63 | - const EVENT_PRIORITY_2 = 2; |
|
| 64 | - const EVENT_PRIORITY_3 = 3; |
|
| 65 | - const EVENT_PRIORITY_4 = 4; |
|
| 66 | - const EVENT_PRIORITY_5 = 5; // Priorité la plus basse |
|
| 58 | + /** |
|
| 59 | + * Priorités des plugins |
|
| 60 | + * @var constant |
|
| 61 | + */ |
|
| 62 | + const EVENT_PRIORITY_1 = 1; // Priorité la plus haute |
|
| 63 | + const EVENT_PRIORITY_2 = 2; |
|
| 64 | + const EVENT_PRIORITY_3 = 3; |
|
| 65 | + const EVENT_PRIORITY_4 = 4; |
|
| 66 | + const EVENT_PRIORITY_5 = 5; // Priorité la plus basse |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Utilisé pour construire le nom de la classe |
|
| 70 | - */ |
|
| 71 | - private $pluginsTypeLabel = array(self::PLUGIN_ACTION => 'Action', self::PLUGIN_FILTER => 'Filter'); |
|
| 68 | + /** |
|
| 69 | + * Utilisé pour construire le nom de la classe |
|
| 70 | + */ |
|
| 71 | + private $pluginsTypeLabel = array(self::PLUGIN_ACTION => 'Action', self::PLUGIN_FILTER => 'Filter'); |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Nom des classes qu'il faut étendre en tant que plugin |
|
| 75 | - */ |
|
| 76 | - private $pluginsClassName = array(self::PLUGIN_ACTION => 'references_action', self::PLUGIN_FILTER => 'references_filter'); |
|
| 73 | + /** |
|
| 74 | + * Nom des classes qu'il faut étendre en tant que plugin |
|
| 75 | + */ |
|
| 76 | + private $pluginsClassName = array(self::PLUGIN_ACTION => 'references_action', self::PLUGIN_FILTER => 'references_filter'); |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Nom de chacun des dossiers en fonction du type de plugin |
|
| 80 | - */ |
|
| 81 | - private $pluginsTypesFolder = array(self::PLUGIN_ACTION => 'actions', self::PLUGIN_FILTER => 'filters'); |
|
| 78 | + /** |
|
| 79 | + * Nom de chacun des dossiers en fonction du type de plugin |
|
| 80 | + */ |
|
| 81 | + private $pluginsTypesFolder = array(self::PLUGIN_ACTION => 'actions', self::PLUGIN_FILTER => 'filters'); |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Contient l'unique instance de l'objet |
|
| 85 | - * @var object |
|
| 86 | - */ |
|
| 87 | - private static $instance = false; |
|
| 83 | + /** |
|
| 84 | + * Contient l'unique instance de l'objet |
|
| 85 | + * @var object |
|
| 86 | + */ |
|
| 87 | + private static $instance = false; |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Liste des évènements |
|
| 91 | - * @var array |
|
| 92 | - */ |
|
| 93 | - private static $events = array(); |
|
| 89 | + /** |
|
| 90 | + * Liste des évènements |
|
| 91 | + * @var array |
|
| 92 | + */ |
|
| 93 | + private static $events = array(); |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * Retourne l'instance unique de la classe |
|
| 97 | - * |
|
| 98 | - * @return object |
|
| 99 | - */ |
|
| 100 | - public static function getInstance() |
|
| 101 | - { |
|
| 102 | - static $instance; |
|
| 103 | - if (null === $instance) { |
|
| 104 | - $instance = new static(); |
|
| 105 | - } |
|
| 95 | + /** |
|
| 96 | + * Retourne l'instance unique de la classe |
|
| 97 | + * |
|
| 98 | + * @return object |
|
| 99 | + */ |
|
| 100 | + public static function getInstance() |
|
| 101 | + { |
|
| 102 | + static $instance; |
|
| 103 | + if (null === $instance) { |
|
| 104 | + $instance = new static(); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - return $instance; |
|
| 108 | - } |
|
| 107 | + return $instance; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Chargement des 2 types de plugins |
|
| 112 | - * |
|
| 113 | - */ |
|
| 114 | - private function __construct() |
|
| 115 | - { |
|
| 116 | - $this->events = array(); |
|
| 117 | - $this->loadPlugins(); |
|
| 118 | - } |
|
| 110 | + /** |
|
| 111 | + * Chargement des 2 types de plugins |
|
| 112 | + * |
|
| 113 | + */ |
|
| 114 | + private function __construct() |
|
| 115 | + { |
|
| 116 | + $this->events = array(); |
|
| 117 | + $this->loadPlugins(); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Chargement des plugins (actions et filtres) |
|
| 122 | - * @return void |
|
| 123 | - */ |
|
| 124 | - public function loadPlugins() |
|
| 125 | - { |
|
| 126 | - $this->loadPluginsFiles(REFERENCES_PLUGINS_PATH . $this->pluginsTypesFolder[self::PLUGIN_ACTION], self::PLUGIN_ACTION); |
|
| 127 | - $this->loadPluginsFiles(REFERENCES_PLUGINS_PATH . $this->pluginsTypesFolder[self::PLUGIN_FILTER], self::PLUGIN_FILTER); |
|
| 128 | - } |
|
| 120 | + /** |
|
| 121 | + * Chargement des plugins (actions et filtres) |
|
| 122 | + * @return void |
|
| 123 | + */ |
|
| 124 | + public function loadPlugins() |
|
| 125 | + { |
|
| 126 | + $this->loadPluginsFiles(REFERENCES_PLUGINS_PATH . $this->pluginsTypesFolder[self::PLUGIN_ACTION], self::PLUGIN_ACTION); |
|
| 127 | + $this->loadPluginsFiles(REFERENCES_PLUGINS_PATH . $this->pluginsTypesFolder[self::PLUGIN_FILTER], self::PLUGIN_FILTER); |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Vérifie que le fichier Php passé en paramètre contient bien une classe de filtre ou d'action et si c'est le cas, le charge dans la liste des plugins |
|
| 132 | - * @param string $fullPathName Chemin complet vers le fichier (répertoire + nom) |
|
| 133 | - * @param integer $type Type de plugin recherché (action ou filtre) |
|
| 134 | - * @param string $pluginFolder Le nom du répertoire dans lequel se trouve le fichier (le "dernier nom") |
|
| 135 | - * @return void |
|
| 136 | - */ |
|
| 137 | - private function loadClass($fullPathName, $type, $pluginFolder) |
|
| 138 | - { |
|
| 139 | - require_once $fullPathName; |
|
| 140 | - // Du style referencesRegionalizationFilter |
|
| 141 | - $className = self::MODULE_DIRNAME . ucfirst(strtolower($pluginFolder)) . $this->pluginsTypeLabel[$type]; |
|
| 142 | - if (class_exists($className) && get_parent_class($className) == $this->pluginsClassName[$type]) { |
|
| 143 | - // TODO: Vérifier que l'évènement n'est pas déjà en mémoire |
|
| 144 | - $events = call_user_func(array($className, self::PLUGIN_DESCRIBE_METHOD)); |
|
| 145 | - foreach ($events as $event) { |
|
| 146 | - $eventName = $event[0]; |
|
| 147 | - $eventPriority = $event[1]; |
|
| 148 | - $fileToInclude = REFERENCES_PLUGINS_PATH . $this->pluginsTypesFolder[$type] . DIRECTORY_SEPARATOR . $pluginFolder . DIRECTORY_SEPARATOR . $event[2]; |
|
| 149 | - $classToCall = $event[3]; |
|
| 150 | - $methodToCall = $event[4]; |
|
| 151 | - $this->events[$type][$eventName][$eventPriority][] = array('fullPathName' => $fileToInclude, 'className' => $classToCall, 'method' => $methodToCall); |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - } |
|
| 130 | + /** |
|
| 131 | + * Vérifie que le fichier Php passé en paramètre contient bien une classe de filtre ou d'action et si c'est le cas, le charge dans la liste des plugins |
|
| 132 | + * @param string $fullPathName Chemin complet vers le fichier (répertoire + nom) |
|
| 133 | + * @param integer $type Type de plugin recherché (action ou filtre) |
|
| 134 | + * @param string $pluginFolder Le nom du répertoire dans lequel se trouve le fichier (le "dernier nom") |
|
| 135 | + * @return void |
|
| 136 | + */ |
|
| 137 | + private function loadClass($fullPathName, $type, $pluginFolder) |
|
| 138 | + { |
|
| 139 | + require_once $fullPathName; |
|
| 140 | + // Du style referencesRegionalizationFilter |
|
| 141 | + $className = self::MODULE_DIRNAME . ucfirst(strtolower($pluginFolder)) . $this->pluginsTypeLabel[$type]; |
|
| 142 | + if (class_exists($className) && get_parent_class($className) == $this->pluginsClassName[$type]) { |
|
| 143 | + // TODO: Vérifier que l'évènement n'est pas déjà en mémoire |
|
| 144 | + $events = call_user_func(array($className, self::PLUGIN_DESCRIBE_METHOD)); |
|
| 145 | + foreach ($events as $event) { |
|
| 146 | + $eventName = $event[0]; |
|
| 147 | + $eventPriority = $event[1]; |
|
| 148 | + $fileToInclude = REFERENCES_PLUGINS_PATH . $this->pluginsTypesFolder[$type] . DIRECTORY_SEPARATOR . $pluginFolder . DIRECTORY_SEPARATOR . $event[2]; |
|
| 149 | + $classToCall = $event[3]; |
|
| 150 | + $methodToCall = $event[4]; |
|
| 151 | + $this->events[$type][$eventName][$eventPriority][] = array('fullPathName' => $fileToInclude, 'className' => $classToCall, 'method' => $methodToCall); |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * Part à la recherche d'un type de plugin dans les répertoires |
|
| 158 | - * |
|
| 159 | - * @param string $path La racine |
|
| 160 | - * @param integer $type Le type de plugin recherché (action ou filtre) |
|
| 161 | - * @return void |
|
| 162 | - */ |
|
| 163 | - private function loadPluginsFiles($path, $type) |
|
| 164 | - { |
|
| 165 | - $objects = new DirectoryIterator($path); |
|
| 166 | - foreach ($objects as $object) { |
|
| 167 | - if ($object->isDir() && !$object->isDot()) { |
|
| 168 | - $file = $path . DIRECTORY_SEPARATOR . $object->current() . DIRECTORY_SEPARATOR . self::PLUGIN_SCRIPT_NAME; |
|
| 169 | - if (file_exists($file)) { |
|
| 170 | - $this->loadClass($file, $type, $object->current()); |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - } |
|
| 156 | + /** |
|
| 157 | + * Part à la recherche d'un type de plugin dans les répertoires |
|
| 158 | + * |
|
| 159 | + * @param string $path La racine |
|
| 160 | + * @param integer $type Le type de plugin recherché (action ou filtre) |
|
| 161 | + * @return void |
|
| 162 | + */ |
|
| 163 | + private function loadPluginsFiles($path, $type) |
|
| 164 | + { |
|
| 165 | + $objects = new DirectoryIterator($path); |
|
| 166 | + foreach ($objects as $object) { |
|
| 167 | + if ($object->isDir() && !$object->isDot()) { |
|
| 168 | + $file = $path . DIRECTORY_SEPARATOR . $object->current() . DIRECTORY_SEPARATOR . self::PLUGIN_SCRIPT_NAME; |
|
| 169 | + if (file_exists($file)) { |
|
| 170 | + $this->loadClass($file, $type, $object->current()); |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | - /** |
|
| 177 | - * Déclenchement d'une action et appel des plugins liés |
|
| 178 | - * |
|
| 179 | - * @param string $eventToFire L'action déclenchée |
|
| 180 | - * @param object|references_parameters $parameters Les paramètres à passer à chaque plugin |
|
| 181 | - * @return object L'objet lui même pour chaîner |
|
| 182 | - */ |
|
| 183 | - public function fireAction($eventToFire, references_parameters $parameters = null) |
|
| 184 | - { |
|
| 185 | - if (!isset($this->events[self::PLUGIN_ACTION][$eventToFire])) { |
|
| 186 | - trigger_error(sprintf(_MD_REFERENCES_PLUGINS_ERROR_1, $eventToFire)); |
|
| 176 | + /** |
|
| 177 | + * Déclenchement d'une action et appel des plugins liés |
|
| 178 | + * |
|
| 179 | + * @param string $eventToFire L'action déclenchée |
|
| 180 | + * @param object|references_parameters $parameters Les paramètres à passer à chaque plugin |
|
| 181 | + * @return object L'objet lui même pour chaîner |
|
| 182 | + */ |
|
| 183 | + public function fireAction($eventToFire, references_parameters $parameters = null) |
|
| 184 | + { |
|
| 185 | + if (!isset($this->events[self::PLUGIN_ACTION][$eventToFire])) { |
|
| 186 | + trigger_error(sprintf(_MD_REFERENCES_PLUGINS_ERROR_1, $eventToFire)); |
|
| 187 | 187 | |
| 188 | - return $this; |
|
| 189 | - } |
|
| 190 | - ksort($this->events[self::PLUGIN_ACTION][$eventToFire]); // Tri par priorit� |
|
| 191 | - foreach ($this->events[self::PLUGIN_ACTION][$eventToFire] as $priority => $events) { |
|
| 192 | - foreach ($events as $event) { |
|
| 193 | - if ($this->isUnplug(self::PLUGIN_ACTION, $eventToFire, $event['fullPathName'], $event['className'], $event['method'])) { |
|
| 194 | - continue; |
|
| 195 | - } |
|
| 196 | - require_once $event['fullPathName']; |
|
| 197 | - if (!class_exists($event['className'])) { |
|
| 198 | - $class = new $event['className']; |
|
| 199 | - } |
|
| 200 | - if (!method_exists($event['className'], $event['method'])) { |
|
| 201 | - continue; |
|
| 202 | - } |
|
| 203 | - call_user_func(array($event['className'], $event['method']), $parameters); |
|
| 204 | - unset($class); |
|
| 205 | - } |
|
| 206 | - } |
|
| 188 | + return $this; |
|
| 189 | + } |
|
| 190 | + ksort($this->events[self::PLUGIN_ACTION][$eventToFire]); // Tri par priorit� |
|
| 191 | + foreach ($this->events[self::PLUGIN_ACTION][$eventToFire] as $priority => $events) { |
|
| 192 | + foreach ($events as $event) { |
|
| 193 | + if ($this->isUnplug(self::PLUGIN_ACTION, $eventToFire, $event['fullPathName'], $event['className'], $event['method'])) { |
|
| 194 | + continue; |
|
| 195 | + } |
|
| 196 | + require_once $event['fullPathName']; |
|
| 197 | + if (!class_exists($event['className'])) { |
|
| 198 | + $class = new $event['className']; |
|
| 199 | + } |
|
| 200 | + if (!method_exists($event['className'], $event['method'])) { |
|
| 201 | + continue; |
|
| 202 | + } |
|
| 203 | + call_user_func(array($event['className'], $event['method']), $parameters); |
|
| 204 | + unset($class); |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - return $this; |
|
| 209 | - } |
|
| 208 | + return $this; |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | - /** |
|
| 212 | - * Déclenchement d'un filtre et appel des plugins liés |
|
| 213 | - * |
|
| 214 | - * @param string $eventToFire Le filtre appelé |
|
| 215 | - * @param object|references_parameters $parameters Les paramètres à passer à chaque plugin |
|
| 216 | - * @return object Le contenu de l'objet passé en paramètre |
|
| 217 | - */ |
|
| 218 | - public function fireFilter($eventToFire, references_parameters $parameters) |
|
| 219 | - { |
|
| 220 | - if (!isset($this->events[self::PLUGIN_FILTER][$eventToFire])) { |
|
| 221 | - trigger_error(sprintf(_MD_REFERENCES_PLUGINS_ERROR_1, $eventToFire)); |
|
| 211 | + /** |
|
| 212 | + * Déclenchement d'un filtre et appel des plugins liés |
|
| 213 | + * |
|
| 214 | + * @param string $eventToFire Le filtre appelé |
|
| 215 | + * @param object|references_parameters $parameters Les paramètres à passer à chaque plugin |
|
| 216 | + * @return object Le contenu de l'objet passé en paramètre |
|
| 217 | + */ |
|
| 218 | + public function fireFilter($eventToFire, references_parameters $parameters) |
|
| 219 | + { |
|
| 220 | + if (!isset($this->events[self::PLUGIN_FILTER][$eventToFire])) { |
|
| 221 | + trigger_error(sprintf(_MD_REFERENCES_PLUGINS_ERROR_1, $eventToFire)); |
|
| 222 | 222 | |
| 223 | - return $this; |
|
| 224 | - } |
|
| 225 | - ksort($this->events[self::PLUGIN_FILTER][$eventToFire]); // Tri par priorité |
|
| 226 | - foreach ($this->events[self::PLUGIN_FILTER][$eventToFire] as $priority => $events) { |
|
| 227 | - foreach ($events as $event) { |
|
| 228 | - if ($this->isUnplug(self::PLUGIN_FILTER, $eventToFire, $event['fullPathName'], $event['className'], $event['method'])) { |
|
| 229 | - continue; |
|
| 230 | - } |
|
| 231 | - require_once $event['fullPathName']; |
|
| 232 | - if (!method_exists($event['className'], $event['method'])) { |
|
| 233 | - continue; |
|
| 234 | - } |
|
| 235 | - //if (!class_exists($event['className'])) { |
|
| 236 | - $class = new $event['className']; |
|
| 237 | - //} |
|
| 238 | - $class->$event['method']($parameters); |
|
| 239 | - //call_user_func(array($event['className'], $event['method']), $parameters); |
|
| 240 | - unset($class); |
|
| 241 | - } |
|
| 242 | - } |
|
| 223 | + return $this; |
|
| 224 | + } |
|
| 225 | + ksort($this->events[self::PLUGIN_FILTER][$eventToFire]); // Tri par priorité |
|
| 226 | + foreach ($this->events[self::PLUGIN_FILTER][$eventToFire] as $priority => $events) { |
|
| 227 | + foreach ($events as $event) { |
|
| 228 | + if ($this->isUnplug(self::PLUGIN_FILTER, $eventToFire, $event['fullPathName'], $event['className'], $event['method'])) { |
|
| 229 | + continue; |
|
| 230 | + } |
|
| 231 | + require_once $event['fullPathName']; |
|
| 232 | + if (!method_exists($event['className'], $event['method'])) { |
|
| 233 | + continue; |
|
| 234 | + } |
|
| 235 | + //if (!class_exists($event['className'])) { |
|
| 236 | + $class = new $event['className']; |
|
| 237 | + //} |
|
| 238 | + $class->$event['method']($parameters); |
|
| 239 | + //call_user_func(array($event['className'], $event['method']), $parameters); |
|
| 240 | + unset($class); |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | 243 | |
| 244 | - if (!is_null($parameters)) { |
|
| 245 | - return $parameters; |
|
| 246 | - } |
|
| 247 | - } |
|
| 244 | + if (!is_null($parameters)) { |
|
| 245 | + return $parameters; |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - /** |
|
| 250 | - * Indique si un plugin s'est détaché d'un évènement particulier |
|
| 251 | - * |
|
| 252 | - * @param integer $eventType |
|
| 253 | - * @param string $eventToFire |
|
| 254 | - * @param string $fullPathName |
|
| 255 | - * @param string $className |
|
| 256 | - * @param string $method |
|
| 257 | - * @return boolean |
|
| 258 | - */ |
|
| 259 | - public function isUnplug($eventType, $eventToFire, $fullPathName, $className, $method) |
|
| 260 | - { |
|
| 261 | - $unplug = array(); |
|
| 262 | - if (isset($_SESSION[self::PLUGIN_UNPLUG_SESSION_NAME])) { |
|
| 263 | - $unplug = $_SESSION[self::PLUGIN_UNPLUG_SESSION_NAME]; |
|
| 264 | - } else { |
|
| 265 | - return false; |
|
| 266 | - } |
|
| 249 | + /** |
|
| 250 | + * Indique si un plugin s'est détaché d'un évènement particulier |
|
| 251 | + * |
|
| 252 | + * @param integer $eventType |
|
| 253 | + * @param string $eventToFire |
|
| 254 | + * @param string $fullPathName |
|
| 255 | + * @param string $className |
|
| 256 | + * @param string $method |
|
| 257 | + * @return boolean |
|
| 258 | + */ |
|
| 259 | + public function isUnplug($eventType, $eventToFire, $fullPathName, $className, $method) |
|
| 260 | + { |
|
| 261 | + $unplug = array(); |
|
| 262 | + if (isset($_SESSION[self::PLUGIN_UNPLUG_SESSION_NAME])) { |
|
| 263 | + $unplug = $_SESSION[self::PLUGIN_UNPLUG_SESSION_NAME]; |
|
| 264 | + } else { |
|
| 265 | + return false; |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - return isset($unplug[$eventType][$eventToFire][$fullPathName][$className][$method]); |
|
| 269 | - } |
|
| 268 | + return isset($unplug[$eventType][$eventToFire][$fullPathName][$className][$method]); |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * Permet à un plugin de se détacher d'un évènement |
|
| 273 | - * |
|
| 274 | - * @param integer $eventType |
|
| 275 | - * @param string $eventToFire |
|
| 276 | - * @param string $fullPathName |
|
| 277 | - * @param string $className |
|
| 278 | - * @param string $method |
|
| 279 | - * @return void |
|
| 280 | - */ |
|
| 281 | - public function unplugFromEvent($eventType, $eventToFire, $fullPathName, $className, $method) |
|
| 282 | - { |
|
| 283 | - $unplug = array(); |
|
| 284 | - if (isset($_SESSION[self::PLUGIN_UNPLUG_SESSION_NAME])) { |
|
| 285 | - $unplug = $_SESSION[self::PLUGIN_UNPLUG_SESSION_NAME]; |
|
| 286 | - } |
|
| 287 | - $unplug[$eventType][$eventToFire][$fullPathName][$className][$method] = true; |
|
| 288 | - $_SESSION[self::PLUGIN_UNPLUG_SESSION_NAME] = $unplug; |
|
| 289 | - } |
|
| 271 | + /** |
|
| 272 | + * Permet à un plugin de se détacher d'un évènement |
|
| 273 | + * |
|
| 274 | + * @param integer $eventType |
|
| 275 | + * @param string $eventToFire |
|
| 276 | + * @param string $fullPathName |
|
| 277 | + * @param string $className |
|
| 278 | + * @param string $method |
|
| 279 | + * @return void |
|
| 280 | + */ |
|
| 281 | + public function unplugFromEvent($eventType, $eventToFire, $fullPathName, $className, $method) |
|
| 282 | + { |
|
| 283 | + $unplug = array(); |
|
| 284 | + if (isset($_SESSION[self::PLUGIN_UNPLUG_SESSION_NAME])) { |
|
| 285 | + $unplug = $_SESSION[self::PLUGIN_UNPLUG_SESSION_NAME]; |
|
| 286 | + } |
|
| 287 | + $unplug[$eventType][$eventToFire][$fullPathName][$className][$method] = true; |
|
| 288 | + $_SESSION[self::PLUGIN_UNPLUG_SESSION_NAME] = $unplug; |
|
| 289 | + } |
|
| 290 | 290 | } |
@@ -28,840 +28,840 @@ |
||
| 28 | 28 | class references_Cache_Lite |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - // --- Private properties --- |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Directory where to put the cache files |
|
| 35 | - * (make sure to add a trailing slash) |
|
| 36 | - * |
|
| 37 | - * @var string $_cacheDir |
|
| 38 | - */ |
|
| 39 | - public $_cacheDir = '/tmp/'; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Enable / disable caching |
|
| 43 | - * |
|
| 44 | - * (can be very usefull for the debug of cached scripts) |
|
| 45 | - * |
|
| 46 | - * @var boolean $_caching |
|
| 47 | - */ |
|
| 48 | - public $_caching = true; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Cache lifetime (in seconds) |
|
| 52 | - * |
|
| 53 | - * If null, the cache is valid forever. |
|
| 54 | - * |
|
| 55 | - * @var int $_lifeTime |
|
| 56 | - */ |
|
| 57 | - public $_lifeTime = 3600; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Enable / disable fileLocking |
|
| 61 | - * |
|
| 62 | - * (can avoid cache corruption under bad circumstances) |
|
| 63 | - * |
|
| 64 | - * @var boolean $_fileLocking |
|
| 65 | - */ |
|
| 66 | - public $_fileLocking = true; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Timestamp of the last valid cache |
|
| 70 | - * |
|
| 71 | - * @var int $_refreshTime |
|
| 72 | - */ |
|
| 73 | - public $_refreshTime; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * File name (with path) |
|
| 77 | - * |
|
| 78 | - * @var string $_file |
|
| 79 | - */ |
|
| 80 | - public $_file; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * File name (without path) |
|
| 84 | - * |
|
| 85 | - * @var string $_fileName |
|
| 86 | - */ |
|
| 87 | - public $_fileName; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Enable / disable write control (the cache is read just after writing to detect corrupt entries) |
|
| 91 | - * |
|
| 92 | - * Enable write control will lightly slow the cache writing but not the cache reading |
|
| 93 | - * Write control can detect some corrupt cache files but maybe it's not a perfect control |
|
| 94 | - * |
|
| 95 | - * @var boolean $_writeControl |
|
| 96 | - */ |
|
| 97 | - public $_writeControl = true; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Enable / disable read control |
|
| 101 | - * |
|
| 102 | - * If enabled, a control key is embeded in cache file and this key is compared with the one |
|
| 103 | - * calculated after the reading. |
|
| 104 | - * |
|
| 105 | - * @var boolean $_writeControl |
|
| 106 | - */ |
|
| 107 | - public $_readControl = true; |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Type of read control (only if read control is enabled) |
|
| 111 | - * |
|
| 112 | - * Available values are : |
|
| 113 | - * 'md5' for a md5 hash control (best but slowest) |
|
| 114 | - * 'crc32' for a crc32 hash control (lightly less safe but faster, better choice) |
|
| 115 | - * 'strlen' for a length only test (fastest) |
|
| 116 | - * |
|
| 117 | - * @var boolean $_readControlType |
|
| 118 | - */ |
|
| 119 | - public $_readControlType = 'crc32'; |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Pear error mode (when raiseError is called) |
|
| 123 | - * |
|
| 124 | - * (see PEAR doc) |
|
| 125 | - * |
|
| 126 | - * @see setToDebug() |
|
| 127 | - * @var int $_pearErrorMode |
|
| 128 | - */ |
|
| 129 | - public $_pearErrorMode = REFERENCES_CACHE_LITE_ERROR_RETURN; |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Current cache id |
|
| 133 | - * |
|
| 134 | - * @var string $_id |
|
| 135 | - */ |
|
| 136 | - public $_id; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Current cache group |
|
| 140 | - * |
|
| 141 | - * @var string $_group |
|
| 142 | - */ |
|
| 143 | - public $_group; |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Enable / Disable "Memory Caching" |
|
| 147 | - * |
|
| 148 | - * NB : There is no lifetime for memory caching ! |
|
| 149 | - * |
|
| 150 | - * @var boolean $_memoryCaching |
|
| 151 | - */ |
|
| 152 | - public $_memoryCaching = false; |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Enable / Disable "Only Memory Caching" |
|
| 156 | - * (be carefull, memory caching is "beta quality") |
|
| 157 | - * |
|
| 158 | - * @var boolean $_onlyMemoryCaching |
|
| 159 | - */ |
|
| 160 | - public $_onlyMemoryCaching = false; |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Memory caching array |
|
| 164 | - * |
|
| 165 | - * @var array $_memoryCachingArray |
|
| 166 | - */ |
|
| 167 | - public $_memoryCachingArray = array(); |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Memory caching counter |
|
| 171 | - * |
|
| 172 | - * @var int $memoryCachingCounter |
|
| 173 | - */ |
|
| 174 | - public $_memoryCachingCounter = 0; |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Memory caching limit |
|
| 178 | - * |
|
| 179 | - * @var int $memoryCachingLimit |
|
| 180 | - */ |
|
| 181 | - public $_memoryCachingLimit = 1000; |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * File Name protection |
|
| 185 | - * |
|
| 186 | - * if set to true, you can use any cache id or group name |
|
| 187 | - * if set to false, it can be faster but cache ids and group names |
|
| 188 | - * will be used directly in cache file names so be carefull with |
|
| 189 | - * special characters... |
|
| 190 | - * |
|
| 191 | - * @var boolean $fileNameProtection |
|
| 192 | - */ |
|
| 193 | - public $_fileNameProtection = true; |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Enable / disable automatic serialization |
|
| 197 | - * |
|
| 198 | - * it can be used to save directly datas which aren't strings |
|
| 199 | - * (but it's slower) |
|
| 200 | - * |
|
| 201 | - * @var boolean $_serialize |
|
| 202 | - */ |
|
| 203 | - public $_automaticSerialization = false; |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Disable / Tune the automatic cleaning process |
|
| 207 | - * |
|
| 208 | - * The automatic cleaning process destroy too old (for the given life time) |
|
| 209 | - * cache files when a new cache file is written. |
|
| 210 | - * 0 => no automatic cache cleaning |
|
| 211 | - * 1 => systematic cache cleaning |
|
| 212 | - * x (integer) > 1 => automatic cleaning randomly 1 times on x cache write |
|
| 213 | - * |
|
| 214 | - * @var int $_automaticCleaning |
|
| 215 | - */ |
|
| 216 | - public $_automaticCleaningFactor = 0; |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Nested directory level |
|
| 220 | - * |
|
| 221 | - * Set the hashed directory structure level. 0 means "no hashed directory |
|
| 222 | - * structure", 1 means "one level of directory", 2 means "two levels"... |
|
| 223 | - * This option can speed up Cache_Lite only when you have many thousands of |
|
| 224 | - * cache file. Only specific benchs can help you to choose the perfect value |
|
| 225 | - * for you. Maybe, 1 or 2 is a good start. |
|
| 226 | - * |
|
| 227 | - * @var int $_hashedDirectoryLevel |
|
| 228 | - */ |
|
| 229 | - public $_hashedDirectoryLevel = 0; |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Umask for hashed directory structure |
|
| 233 | - * |
|
| 234 | - * @var int $_hashedDirectoryUmask |
|
| 235 | - */ |
|
| 236 | - public $_hashedDirectoryUmask = 0700; |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * API break for error handling in REFERENCES_CACHE_LITE_ERROR_RETURN mode |
|
| 240 | - * |
|
| 241 | - * In REFERENCES_CACHE_LITE_ERROR_RETURN mode, error handling was not good because |
|
| 242 | - * for example save() method always returned a boolean (a PEAR_Error object |
|
| 243 | - * would be better in REFERENCES_CACHE_LITE_ERROR_RETURN mode). To correct this without |
|
| 244 | - * breaking the API, this option (false by default) can change this handling. |
|
| 245 | - * |
|
| 246 | - * @var boolean |
|
| 247 | - */ |
|
| 248 | - public $_errorHandlingAPIBreak = false; |
|
| 249 | - |
|
| 250 | - // --- Public methods --- |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Constructor |
|
| 254 | - * |
|
| 255 | - * $options is an assoc. Available options are : |
|
| 256 | - * $options = array( |
|
| 257 | - * 'cacheDir' => directory where to put the cache files (string) , |
|
| 258 | - * 'caching' => enable / disable caching (boolean) , |
|
| 259 | - * 'lifeTime' => cache lifetime in seconds (int) , |
|
| 260 | - * 'fileLocking' => enable / disable fileLocking (boolean) , |
|
| 261 | - * 'writeControl' => enable / disable write control (boolean) , |
|
| 262 | - * 'readControl' => enable / disable read control (boolean) , |
|
| 263 | - * 'readControlType' => type of read control 'crc32', 'md5', 'strlen' (string) , |
|
| 264 | - * 'pearErrorMode' => pear error mode (when raiseError is called) (cf PEAR doc) (int) , |
|
| 265 | - * 'memoryCaching' => enable / disable memory caching (boolean) , |
|
| 266 | - * 'onlyMemoryCaching' => enable / disable only memory caching (boolean) , |
|
| 267 | - * 'memoryCachingLimit' => max nbr of records to store into memory caching (int) , |
|
| 268 | - * 'fileNameProtection' => enable / disable automatic file name protection (boolean) , |
|
| 269 | - * 'automaticSerialization' => enable / disable automatic serialization (boolean) , |
|
| 270 | - * 'automaticCleaningFactor' => distable / tune automatic cleaning process (int) , |
|
| 271 | - * 'hashedDirectoryLevel' => level of the hashed directory system (int) , |
|
| 272 | - * 'hashedDirectoryUmask' => umask for hashed directory structure (int) , |
|
| 273 | - * 'errorHandlingAPIBreak' => API break for better error handling ? (boolean) |
|
| 274 | - * ); |
|
| 275 | - * |
|
| 276 | - * @param array $options options |
|
| 277 | - * @access public |
|
| 278 | - */ |
|
| 279 | - public function __construct($options = array(null)) |
|
| 280 | - { |
|
| 281 | - foreach ($options as $key => $value) { |
|
| 282 | - $this->setOption($key, $value); |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Generic way to set a Cache_Lite option |
|
| 288 | - * |
|
| 289 | - * see Cache_Lite constructor for available options |
|
| 290 | - * |
|
| 291 | - * @var string $name name of the option |
|
| 292 | - * @var mixed $value value of the option |
|
| 293 | - * @access public |
|
| 294 | - */ |
|
| 295 | - public function setOption($name, $value) |
|
| 296 | - { |
|
| 297 | - $availableOptions = array( |
|
| 298 | - 'errorHandlingAPIBreak', |
|
| 299 | - 'hashedDirectoryUmask', |
|
| 300 | - 'hashedDirectoryLevel', |
|
| 301 | - 'automaticCleaningFactor', |
|
| 302 | - 'automaticSerialization', |
|
| 303 | - 'fileNameProtection', |
|
| 304 | - 'memoryCaching', |
|
| 305 | - 'onlyMemoryCaching', |
|
| 306 | - 'memoryCachingLimit', |
|
| 307 | - 'cacheDir', |
|
| 308 | - 'caching', |
|
| 309 | - 'lifeTime', |
|
| 310 | - 'fileLocking', |
|
| 311 | - 'writeControl', |
|
| 312 | - 'readControl', |
|
| 313 | - 'readControlType', |
|
| 314 | - 'pearErrorMode' |
|
| 315 | - ); |
|
| 316 | - if (in_array($name, $availableOptions)) { |
|
| 317 | - $property = '_' . $name; |
|
| 318 | - $this->$property = $value; |
|
| 319 | - } |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Test if a cache is available and (if yes) return it |
|
| 324 | - * |
|
| 325 | - * @param string $id cache id |
|
| 326 | - * @param string $group name of the cache group |
|
| 327 | - * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested |
|
| 328 | - * @return string data of the cache (else : false) |
|
| 329 | - * @access public |
|
| 330 | - */ |
|
| 331 | - public function get($id, $group = 'default', $doNotTestCacheValidity = false) |
|
| 332 | - { |
|
| 333 | - $this->_id = $id; |
|
| 334 | - $this->_group = $group; |
|
| 335 | - $data = false; |
|
| 336 | - if ($this->_caching) { |
|
| 337 | - $this->_setRefreshTime(); |
|
| 338 | - $this->_setFileName($id, $group); |
|
| 339 | - clearstatcache(); |
|
| 340 | - if ($this->_memoryCaching) { |
|
| 341 | - if (isset($this->_memoryCachingArray[$this->_file])) { |
|
| 342 | - if ($this->_automaticSerialization) { |
|
| 343 | - return unserialize($this->_memoryCachingArray[$this->_file]); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - return $this->_memoryCachingArray[$this->_file]; |
|
| 347 | - } |
|
| 348 | - if ($this->_onlyMemoryCaching) { |
|
| 349 | - return false; |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - if ($doNotTestCacheValidity || is_null($this->_refreshTime)) { |
|
| 353 | - if (file_exists($this->_file)) { |
|
| 354 | - $data = $this->_read(); |
|
| 355 | - } |
|
| 356 | - } else { |
|
| 357 | - if (file_exists($this->_file) && (@filemtime($this->_file) > $this->_refreshTime)) { |
|
| 358 | - $data = $this->_read(); |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - if ($data && $this->_memoryCaching) { |
|
| 362 | - $this->_memoryCacheAdd($data); |
|
| 363 | - } |
|
| 364 | - if ($this->_automaticSerialization && is_string($data)) { |
|
| 365 | - $data = unserialize($data); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - return $data; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return false; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Save some data in a cache file |
|
| 376 | - * |
|
| 377 | - * @param string $data data to put in cache (can be another type than strings if automaticSerialization is on) |
|
| 378 | - * @param string $id cache id |
|
| 379 | - * @param string $group name of the cache group |
|
| 380 | - * @return boolean true if no problem (else : false or a PEAR_Error object) |
|
| 381 | - * @access public |
|
| 382 | - */ |
|
| 383 | - public function save($data, $id = null, $group = 'default') |
|
| 384 | - { |
|
| 385 | - if ($this->_caching) { |
|
| 386 | - if ($this->_automaticSerialization) { |
|
| 387 | - $data = serialize($data); |
|
| 388 | - } |
|
| 389 | - if (isset($id)) { |
|
| 390 | - $this->_setFileName($id, $group); |
|
| 391 | - } |
|
| 392 | - if ($this->_memoryCaching) { |
|
| 393 | - $this->_memoryCacheAdd($data); |
|
| 394 | - if ($this->_onlyMemoryCaching) { |
|
| 395 | - return true; |
|
| 396 | - } |
|
| 397 | - } |
|
| 398 | - if ($this->_automaticCleaningFactor > 0 && ($this->_automaticCleaningFactor == 1 || mt_rand(1, $this->_automaticCleaningFactor) == 1)) { |
|
| 399 | - $this->clean(false, 'old'); |
|
| 400 | - } |
|
| 401 | - if ($this->_writeControl) { |
|
| 402 | - $res = $this->_writeAndControl($data); |
|
| 403 | - if (is_bool($res)) { |
|
| 404 | - if ($res) { |
|
| 405 | - return true; |
|
| 406 | - } |
|
| 407 | - // if $res if false, we need to invalidate the cache |
|
| 408 | - @touch($this->_file, time() - 2 * abs($this->_lifeTime)); |
|
| 409 | - |
|
| 410 | - return false; |
|
| 411 | - } |
|
| 412 | - } else { |
|
| 413 | - $res = $this->_write($data); |
|
| 414 | - } |
|
| 415 | - if (is_object($res)) { |
|
| 416 | - // $res is a PEAR_Error object |
|
| 417 | - if (!$this->_errorHandlingAPIBreak) { |
|
| 418 | - return false; // we return false (old API) |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - return $res; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - return false; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * Remove a cache file |
|
| 430 | - * |
|
| 431 | - * @param string $id cache id |
|
| 432 | - * @param string $group name of the cache group |
|
| 433 | - * @param boolean $checkbeforeunlink check if file exists before removing it |
|
| 434 | - * @return boolean true if no problem |
|
| 435 | - * @access public |
|
| 436 | - */ |
|
| 437 | - public function remove($id, $group = 'default', $checkbeforeunlink = false) |
|
| 438 | - { |
|
| 439 | - $this->_setFileName($id, $group); |
|
| 440 | - if ($this->_memoryCaching) { |
|
| 441 | - if (isset($this->_memoryCachingArray[$this->_file])) { |
|
| 442 | - unset($this->_memoryCachingArray[$this->_file]); |
|
| 443 | - --$this->_memoryCachingCounter; |
|
| 444 | - } |
|
| 445 | - if ($this->_onlyMemoryCaching) { |
|
| 446 | - return true; |
|
| 447 | - } |
|
| 448 | - } |
|
| 449 | - if ($checkbeforeunlink) { |
|
| 450 | - if (!file_exists($this->_file)) { |
|
| 451 | - return true; |
|
| 452 | - } |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - return $this->_unlink($this->_file); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * Clean the cache |
|
| 460 | - * |
|
| 461 | - * if no group is specified all cache files will be destroyed |
|
| 462 | - * else only cache files of the specified group will be destroyed |
|
| 463 | - * |
|
| 464 | - * @param bool|string $group name of the cache group |
|
| 465 | - * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup', |
|
| 466 | - * 'callback_myFunction' |
|
| 467 | - * @return bool true if no problem |
|
| 468 | - * @access public |
|
| 469 | - */ |
|
| 470 | - public function clean($group = false, $mode = 'ingroup') |
|
| 471 | - { |
|
| 472 | - return $this->_cleanDir($this->_cacheDir, $group, $mode); |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * Set to debug mode |
|
| 477 | - * |
|
| 478 | - * When an error is found, the script will stop and the message will be displayed |
|
| 479 | - * (in debug mode only). |
|
| 480 | - * |
|
| 481 | - * @access public |
|
| 482 | - */ |
|
| 483 | - public function setToDebug() |
|
| 484 | - { |
|
| 485 | - $this->setOption('pearErrorMode', REFERENCES_CACHE_LITE_ERROR_DIE); |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * Set a new life time |
|
| 490 | - * |
|
| 491 | - * @param int $newLifeTime new life time (in seconds) |
|
| 492 | - * @access public |
|
| 493 | - */ |
|
| 494 | - public function setLifeTime($newLifeTime) |
|
| 495 | - { |
|
| 496 | - $this->_lifeTime = $newLifeTime; |
|
| 497 | - $this->_setRefreshTime(); |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - /** |
|
| 501 | - * Save the state of the caching memory array into a cache file cache |
|
| 502 | - * |
|
| 503 | - * @param string $id cache id |
|
| 504 | - * @param string $group name of the cache group |
|
| 505 | - * @access public |
|
| 506 | - */ |
|
| 507 | - public function saveMemoryCachingState($id, $group = 'default') |
|
| 508 | - { |
|
| 509 | - if ($this->_caching) { |
|
| 510 | - $array = array( |
|
| 511 | - 'counter' => $this->_memoryCachingCounter, |
|
| 512 | - 'array' => $this->_memoryCachingArray |
|
| 513 | - ); |
|
| 514 | - $data = serialize($array); |
|
| 515 | - $this->save($data, $id, $group); |
|
| 516 | - } |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * Load the state of the caching memory array from a given cache file cache |
|
| 521 | - * |
|
| 522 | - * @param string $id cache id |
|
| 523 | - * @param string $group name of the cache group |
|
| 524 | - * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested |
|
| 525 | - * @access public |
|
| 526 | - */ |
|
| 527 | - public function getMemoryCachingState($id, $group = 'default', $doNotTestCacheValidity = false) |
|
| 528 | - { |
|
| 529 | - if ($this->_caching) { |
|
| 530 | - if ($data = $this->get($id, $group, $doNotTestCacheValidity)) { |
|
| 531 | - $array = unserialize($data); |
|
| 532 | - $this->_memoryCachingCounter = $array['counter']; |
|
| 533 | - $this->_memoryCachingArray = $array['array']; |
|
| 534 | - } |
|
| 535 | - } |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Return the cache last modification time |
|
| 540 | - * |
|
| 541 | - * BE CAREFUL : THIS METHOD IS FOR HACKING ONLY ! |
|
| 542 | - * |
|
| 543 | - * @return int last modification time |
|
| 544 | - */ |
|
| 545 | - public function lastModified() |
|
| 546 | - { |
|
| 547 | - return @filemtime($this->_file); |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * Trigger a PEAR error |
|
| 552 | - * |
|
| 553 | - * To improve performances, the PEAR.php file is included dynamically. |
|
| 554 | - * The file is so included only when an error is triggered. So, in most |
|
| 555 | - * cases, the file isn't included and perfs are much better. |
|
| 556 | - * |
|
| 557 | - * @param string $msg error message |
|
| 558 | - * @param int $code error code |
|
| 559 | - * @access public |
|
| 560 | - * @return object |
|
| 561 | - */ |
|
| 562 | - public function raiseError($msg, $code) |
|
| 563 | - { |
|
| 564 | - include_once('PEAR.php'); |
|
| 565 | - |
|
| 566 | - return references_PEAR::raiseError($msg, $code, $this->_pearErrorMode); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Extend the life of a valid cache file |
|
| 571 | - * |
|
| 572 | - * see http://pear.php.net/bugs/bug.php?id=6681 |
|
| 573 | - * |
|
| 574 | - * @access public |
|
| 575 | - */ |
|
| 576 | - public function extendLife() |
|
| 577 | - { |
|
| 578 | - @touch($this->_file); |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - // --- Private methods --- |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * Compute & set the refresh time |
|
| 585 | - * |
|
| 586 | - * @access private |
|
| 587 | - */ |
|
| 588 | - public function _setRefreshTime() |
|
| 589 | - { |
|
| 590 | - if (is_null($this->_lifeTime)) { |
|
| 591 | - $this->_refreshTime = null; |
|
| 592 | - } else { |
|
| 593 | - $this->_refreshTime = time() - $this->_lifeTime; |
|
| 594 | - } |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * Remove a file |
|
| 599 | - * |
|
| 600 | - * @param string $file complete file path and name |
|
| 601 | - * @return boolean true if no problem |
|
| 602 | - * @access private |
|
| 603 | - */ |
|
| 604 | - public function _unlink($file) |
|
| 605 | - { |
|
| 606 | - if (!@unlink($file)) { |
|
| 607 | - return $this->raiseError('Cache_Lite : Unable to remove cache !', -3); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - return true; |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - /** |
|
| 614 | - * Recursive function for cleaning cache file in the given directory |
|
| 615 | - * |
|
| 616 | - * @param string $dir directory complete path (with a trailing slash) |
|
| 617 | - * @param bool|string $group name of the cache group |
|
| 618 | - * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup', |
|
| 619 | - * 'callback_myFunction' |
|
| 620 | - * @return bool true if no problem |
|
| 621 | - * @access private |
|
| 622 | - */ |
|
| 623 | - public function _cleanDir($dir, $group = false, $mode = 'ingroup') |
|
| 624 | - { |
|
| 625 | - if ($this->_fileNameProtection) { |
|
| 626 | - $motif = $group ? 'cache_' . md5($group) . '_' : 'cache_'; |
|
| 627 | - } else { |
|
| 628 | - $motif = $group ? 'cache_' . $group . '_' : 'cache_'; |
|
| 629 | - } |
|
| 630 | - if ($this->_memoryCaching) { |
|
| 631 | - foreach ($this->_memoryCachingArray as $key => $v) { |
|
| 632 | - if (strpos($key, $motif) !== false) { |
|
| 633 | - unset($this->_memoryCachingArray[$key]); |
|
| 634 | - --$this->_memoryCachingCounter; |
|
| 635 | - } |
|
| 636 | - } |
|
| 637 | - if ($this->_onlyMemoryCaching) { |
|
| 638 | - return true; |
|
| 639 | - } |
|
| 640 | - } |
|
| 641 | - if (!($dh = opendir($dir))) { |
|
| 642 | - return $this->raiseError('Cache_Lite : Unable to open cache directory !', -4); |
|
| 643 | - } |
|
| 644 | - $result = true; |
|
| 645 | - while ($file = readdir($dh)) { |
|
| 646 | - if (($file !== '.') && ($file !== '..')) { |
|
| 647 | - if (substr($file, 0, 6) === 'cache_') { |
|
| 648 | - $file2 = $dir . $file; |
|
| 649 | - if (is_file($file2)) { |
|
| 650 | - switch (substr($mode, 0, 9)) { |
|
| 651 | - case 'old': |
|
| 652 | - // files older than lifeTime get deleted from cache |
|
| 653 | - if (!is_null($this->_lifeTime)) { |
|
| 654 | - if ((time() - @filemtime($file2)) > $this->_lifeTime) { |
|
| 655 | - $result = ($result and $this->_unlink($file2)); |
|
| 656 | - } |
|
| 657 | - } |
|
| 658 | - break; |
|
| 659 | - case 'notingrou': |
|
| 660 | - if (strpos($file2, $motif) === false) { |
|
| 661 | - $result = ($result and $this->_unlink($file2)); |
|
| 662 | - } |
|
| 663 | - break; |
|
| 664 | - case 'callback_': |
|
| 665 | - $func = substr($mode, 9, strlen($mode) - 9); |
|
| 666 | - if ($func($file2, $group)) { |
|
| 667 | - $result = ($result and $this->_unlink($file2)); |
|
| 668 | - } |
|
| 669 | - break; |
|
| 670 | - case 'ingroup': |
|
| 671 | - default: |
|
| 672 | - if (strpos($file2, $motif) !== false) { |
|
| 673 | - $result = ($result and $this->_unlink($file2)); |
|
| 674 | - } |
|
| 675 | - break; |
|
| 676 | - } |
|
| 677 | - } |
|
| 678 | - if (is_dir($file2) and ($this->_hashedDirectoryLevel > 0)) { |
|
| 679 | - $result = ($result and $this->_cleanDir($file2 . '/', $group, $mode)); |
|
| 680 | - } |
|
| 681 | - } |
|
| 682 | - } |
|
| 683 | - } |
|
| 684 | - |
|
| 685 | - return $result; |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - /** |
|
| 689 | - * Add some date in the memory caching array |
|
| 690 | - * |
|
| 691 | - * @param string $data data to cache |
|
| 692 | - * @access private |
|
| 693 | - */ |
|
| 694 | - public function _memoryCacheAdd($data) |
|
| 695 | - { |
|
| 696 | - $this->_memoryCachingArray[$this->_file] = $data; |
|
| 697 | - if ($this->_memoryCachingCounter >= $this->_memoryCachingLimit) { |
|
| 698 | - list($key,) = each($this->_memoryCachingArray); |
|
| 699 | - unset($this->_memoryCachingArray[$key]); |
|
| 700 | - } else { |
|
| 701 | - ++$this->_memoryCachingCounter; |
|
| 702 | - } |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * Make a file name (with path) |
|
| 707 | - * |
|
| 708 | - * @param string $id cache id |
|
| 709 | - * @param string $group name of the group |
|
| 710 | - * @access private |
|
| 711 | - */ |
|
| 712 | - public function _setFileName($id, $group) |
|
| 713 | - { |
|
| 714 | - if ($this->_fileNameProtection) { |
|
| 715 | - $suffix = 'cache_' . md5($group) . '_' . md5($id); |
|
| 716 | - } else { |
|
| 717 | - $suffix = 'cache_' . $group . '_' . $id; |
|
| 718 | - } |
|
| 719 | - $root = $this->_cacheDir; |
|
| 720 | - if ($this->_hashedDirectoryLevel > 0) { |
|
| 721 | - $hash = md5($suffix); |
|
| 722 | - for ($i = 0; $i < $this->_hashedDirectoryLevel; ++$i) { |
|
| 723 | - $root = $root . 'cache_' . substr($hash, 0, $i + 1) . '/'; |
|
| 724 | - } |
|
| 725 | - } |
|
| 726 | - $this->_fileName = $suffix; |
|
| 727 | - $this->_file = $root . $suffix; |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - /** |
|
| 731 | - * Read the cache file and return the content |
|
| 732 | - * |
|
| 733 | - * @return string content of the cache file (else : false or a PEAR_Error object) |
|
| 734 | - * @access private |
|
| 735 | - */ |
|
| 736 | - public function _read() |
|
| 737 | - { |
|
| 738 | - $fp = @fopen($this->_file, 'rb'); |
|
| 739 | - if ($this->_fileLocking) { |
|
| 740 | - @flock($fp, LOCK_SH); |
|
| 741 | - } |
|
| 742 | - if ($fp) { |
|
| 743 | - clearstatcache(); |
|
| 744 | - $length = @filesize($this->_file); |
|
| 745 | - // $mqr = get_magic_quotes_runtime(); |
|
| 746 | - // @set_magic_quotes_runtime(0); |
|
| 747 | - if ($this->_readControl) { |
|
| 748 | - $hashControl = @fread($fp, 32); |
|
| 749 | - $length = $length - 32; |
|
| 750 | - } |
|
| 751 | - if ($length) { |
|
| 752 | - $data = @fread($fp, $length); |
|
| 753 | - } else { |
|
| 754 | - $data = ''; |
|
| 755 | - } |
|
| 756 | - // @set_magic_quotes_runtime($mqr); |
|
| 757 | - if ($this->_fileLocking) { |
|
| 758 | - @flock($fp, LOCK_UN); |
|
| 759 | - } |
|
| 760 | - @fclose($fp); |
|
| 761 | - if ($this->_readControl) { |
|
| 762 | - $hashData = $this->_hash($data, $this->_readControlType); |
|
| 763 | - if ($hashData != $hashControl) { |
|
| 764 | - if (!is_null($this->_lifeTime)) { |
|
| 765 | - @touch($this->_file, time() - 2 * abs($this->_lifeTime)); |
|
| 766 | - } else { |
|
| 767 | - @unlink($this->_file); |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - return false; |
|
| 771 | - } |
|
| 772 | - } |
|
| 773 | - |
|
| 774 | - return $data; |
|
| 775 | - } |
|
| 776 | - |
|
| 777 | - return $this->raiseError('Cache_Lite : Unable to read cache !', -2); |
|
| 778 | - } |
|
| 779 | - |
|
| 780 | - /** |
|
| 781 | - * Write the given data in the cache file |
|
| 782 | - * |
|
| 783 | - * @param string $data data to put in cache |
|
| 784 | - * @return boolean true if ok (a PEAR_Error object else) |
|
| 785 | - * @access private |
|
| 786 | - */ |
|
| 787 | - public function _write($data) |
|
| 788 | - { |
|
| 789 | - if ($this->_hashedDirectoryLevel > 0) { |
|
| 790 | - $hash = md5($this->_fileName); |
|
| 791 | - $root = $this->_cacheDir; |
|
| 792 | - for ($i = 0; $i < $this->_hashedDirectoryLevel; ++$i) { |
|
| 793 | - $root = $root . 'cache_' . substr($hash, 0, $i + 1) . '/'; |
|
| 794 | - if (!(@is_dir($root))) { |
|
| 795 | - @mkdir($root, $this->_hashedDirectoryUmask); |
|
| 796 | - } |
|
| 797 | - } |
|
| 798 | - } |
|
| 799 | - $fp = @fopen($this->_file, 'wb'); |
|
| 800 | - if ($fp) { |
|
| 801 | - if ($this->_fileLocking) { |
|
| 802 | - @flock($fp, LOCK_EX); |
|
| 803 | - } |
|
| 804 | - if ($this->_readControl) { |
|
| 805 | - @fwrite($fp, $this->_hash($data, $this->_readControlType), 32); |
|
| 806 | - } |
|
| 807 | - // $mqr = get_magic_quotes_runtime(); |
|
| 808 | - // @set_magic_quotes_runtime(0); |
|
| 809 | - @fwrite($fp, $data); |
|
| 810 | - // @set_magic_quotes_runtime($mqr); |
|
| 811 | - if ($this->_fileLocking) { |
|
| 812 | - @flock($fp, LOCK_UN); |
|
| 813 | - } |
|
| 814 | - @fclose($fp); |
|
| 815 | - |
|
| 816 | - return true; |
|
| 817 | - } |
|
| 818 | - |
|
| 819 | - return $this->raiseError('Cache_Lite : Unable to write cache file : ' . $this->_file, -1); |
|
| 820 | - } |
|
| 821 | - |
|
| 822 | - /** |
|
| 823 | - * Write the given data in the cache file and control it just after to avoir corrupted cache entries |
|
| 824 | - * |
|
| 825 | - * @param string $data data to put in cache |
|
| 826 | - * @return boolean true if the test is ok (else : false or a PEAR_Error object) |
|
| 827 | - * @access private |
|
| 828 | - */ |
|
| 829 | - public function _writeAndControl($data) |
|
| 830 | - { |
|
| 831 | - $result = $this->_write($data); |
|
| 832 | - if (is_object($result)) { |
|
| 833 | - return $result; # We return the PEAR_Error object |
|
| 834 | - } |
|
| 835 | - $dataRead = $this->_read(); |
|
| 836 | - if (is_object($dataRead)) { |
|
| 837 | - return $dataRead; # We return the PEAR_Error object |
|
| 838 | - } |
|
| 839 | - if (is_bool($dataRead) && (!$dataRead)) { |
|
| 840 | - return false; |
|
| 841 | - } |
|
| 842 | - |
|
| 843 | - return ($dataRead == $data); |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - /** |
|
| 847 | - * Make a control key with the string containing datas |
|
| 848 | - * |
|
| 849 | - * @param string $data data |
|
| 850 | - * @param string $controlType type of control 'md5', 'crc32' or 'strlen' |
|
| 851 | - * @return string control key |
|
| 852 | - * @access private |
|
| 853 | - */ |
|
| 854 | - public function _hash($data, $controlType) |
|
| 855 | - { |
|
| 856 | - switch ($controlType) { |
|
| 857 | - case 'md5': |
|
| 858 | - return md5($data); |
|
| 859 | - case 'crc32': |
|
| 860 | - return sprintf('% 32d', crc32($data)); |
|
| 861 | - case 'strlen': |
|
| 862 | - return sprintf('% 32d', strlen($data)); |
|
| 863 | - default: |
|
| 864 | - return $this->raiseError('Unknown controlType ! (available values are only \'md5\', \'crc32\', \'strlen\')', -5); |
|
| 865 | - } |
|
| 866 | - } |
|
| 31 | + // --- Private properties --- |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Directory where to put the cache files |
|
| 35 | + * (make sure to add a trailing slash) |
|
| 36 | + * |
|
| 37 | + * @var string $_cacheDir |
|
| 38 | + */ |
|
| 39 | + public $_cacheDir = '/tmp/'; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Enable / disable caching |
|
| 43 | + * |
|
| 44 | + * (can be very usefull for the debug of cached scripts) |
|
| 45 | + * |
|
| 46 | + * @var boolean $_caching |
|
| 47 | + */ |
|
| 48 | + public $_caching = true; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Cache lifetime (in seconds) |
|
| 52 | + * |
|
| 53 | + * If null, the cache is valid forever. |
|
| 54 | + * |
|
| 55 | + * @var int $_lifeTime |
|
| 56 | + */ |
|
| 57 | + public $_lifeTime = 3600; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Enable / disable fileLocking |
|
| 61 | + * |
|
| 62 | + * (can avoid cache corruption under bad circumstances) |
|
| 63 | + * |
|
| 64 | + * @var boolean $_fileLocking |
|
| 65 | + */ |
|
| 66 | + public $_fileLocking = true; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Timestamp of the last valid cache |
|
| 70 | + * |
|
| 71 | + * @var int $_refreshTime |
|
| 72 | + */ |
|
| 73 | + public $_refreshTime; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * File name (with path) |
|
| 77 | + * |
|
| 78 | + * @var string $_file |
|
| 79 | + */ |
|
| 80 | + public $_file; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * File name (without path) |
|
| 84 | + * |
|
| 85 | + * @var string $_fileName |
|
| 86 | + */ |
|
| 87 | + public $_fileName; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Enable / disable write control (the cache is read just after writing to detect corrupt entries) |
|
| 91 | + * |
|
| 92 | + * Enable write control will lightly slow the cache writing but not the cache reading |
|
| 93 | + * Write control can detect some corrupt cache files but maybe it's not a perfect control |
|
| 94 | + * |
|
| 95 | + * @var boolean $_writeControl |
|
| 96 | + */ |
|
| 97 | + public $_writeControl = true; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Enable / disable read control |
|
| 101 | + * |
|
| 102 | + * If enabled, a control key is embeded in cache file and this key is compared with the one |
|
| 103 | + * calculated after the reading. |
|
| 104 | + * |
|
| 105 | + * @var boolean $_writeControl |
|
| 106 | + */ |
|
| 107 | + public $_readControl = true; |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Type of read control (only if read control is enabled) |
|
| 111 | + * |
|
| 112 | + * Available values are : |
|
| 113 | + * 'md5' for a md5 hash control (best but slowest) |
|
| 114 | + * 'crc32' for a crc32 hash control (lightly less safe but faster, better choice) |
|
| 115 | + * 'strlen' for a length only test (fastest) |
|
| 116 | + * |
|
| 117 | + * @var boolean $_readControlType |
|
| 118 | + */ |
|
| 119 | + public $_readControlType = 'crc32'; |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Pear error mode (when raiseError is called) |
|
| 123 | + * |
|
| 124 | + * (see PEAR doc) |
|
| 125 | + * |
|
| 126 | + * @see setToDebug() |
|
| 127 | + * @var int $_pearErrorMode |
|
| 128 | + */ |
|
| 129 | + public $_pearErrorMode = REFERENCES_CACHE_LITE_ERROR_RETURN; |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Current cache id |
|
| 133 | + * |
|
| 134 | + * @var string $_id |
|
| 135 | + */ |
|
| 136 | + public $_id; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Current cache group |
|
| 140 | + * |
|
| 141 | + * @var string $_group |
|
| 142 | + */ |
|
| 143 | + public $_group; |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Enable / Disable "Memory Caching" |
|
| 147 | + * |
|
| 148 | + * NB : There is no lifetime for memory caching ! |
|
| 149 | + * |
|
| 150 | + * @var boolean $_memoryCaching |
|
| 151 | + */ |
|
| 152 | + public $_memoryCaching = false; |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Enable / Disable "Only Memory Caching" |
|
| 156 | + * (be carefull, memory caching is "beta quality") |
|
| 157 | + * |
|
| 158 | + * @var boolean $_onlyMemoryCaching |
|
| 159 | + */ |
|
| 160 | + public $_onlyMemoryCaching = false; |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Memory caching array |
|
| 164 | + * |
|
| 165 | + * @var array $_memoryCachingArray |
|
| 166 | + */ |
|
| 167 | + public $_memoryCachingArray = array(); |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Memory caching counter |
|
| 171 | + * |
|
| 172 | + * @var int $memoryCachingCounter |
|
| 173 | + */ |
|
| 174 | + public $_memoryCachingCounter = 0; |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Memory caching limit |
|
| 178 | + * |
|
| 179 | + * @var int $memoryCachingLimit |
|
| 180 | + */ |
|
| 181 | + public $_memoryCachingLimit = 1000; |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * File Name protection |
|
| 185 | + * |
|
| 186 | + * if set to true, you can use any cache id or group name |
|
| 187 | + * if set to false, it can be faster but cache ids and group names |
|
| 188 | + * will be used directly in cache file names so be carefull with |
|
| 189 | + * special characters... |
|
| 190 | + * |
|
| 191 | + * @var boolean $fileNameProtection |
|
| 192 | + */ |
|
| 193 | + public $_fileNameProtection = true; |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Enable / disable automatic serialization |
|
| 197 | + * |
|
| 198 | + * it can be used to save directly datas which aren't strings |
|
| 199 | + * (but it's slower) |
|
| 200 | + * |
|
| 201 | + * @var boolean $_serialize |
|
| 202 | + */ |
|
| 203 | + public $_automaticSerialization = false; |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Disable / Tune the automatic cleaning process |
|
| 207 | + * |
|
| 208 | + * The automatic cleaning process destroy too old (for the given life time) |
|
| 209 | + * cache files when a new cache file is written. |
|
| 210 | + * 0 => no automatic cache cleaning |
|
| 211 | + * 1 => systematic cache cleaning |
|
| 212 | + * x (integer) > 1 => automatic cleaning randomly 1 times on x cache write |
|
| 213 | + * |
|
| 214 | + * @var int $_automaticCleaning |
|
| 215 | + */ |
|
| 216 | + public $_automaticCleaningFactor = 0; |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Nested directory level |
|
| 220 | + * |
|
| 221 | + * Set the hashed directory structure level. 0 means "no hashed directory |
|
| 222 | + * structure", 1 means "one level of directory", 2 means "two levels"... |
|
| 223 | + * This option can speed up Cache_Lite only when you have many thousands of |
|
| 224 | + * cache file. Only specific benchs can help you to choose the perfect value |
|
| 225 | + * for you. Maybe, 1 or 2 is a good start. |
|
| 226 | + * |
|
| 227 | + * @var int $_hashedDirectoryLevel |
|
| 228 | + */ |
|
| 229 | + public $_hashedDirectoryLevel = 0; |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Umask for hashed directory structure |
|
| 233 | + * |
|
| 234 | + * @var int $_hashedDirectoryUmask |
|
| 235 | + */ |
|
| 236 | + public $_hashedDirectoryUmask = 0700; |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * API break for error handling in REFERENCES_CACHE_LITE_ERROR_RETURN mode |
|
| 240 | + * |
|
| 241 | + * In REFERENCES_CACHE_LITE_ERROR_RETURN mode, error handling was not good because |
|
| 242 | + * for example save() method always returned a boolean (a PEAR_Error object |
|
| 243 | + * would be better in REFERENCES_CACHE_LITE_ERROR_RETURN mode). To correct this without |
|
| 244 | + * breaking the API, this option (false by default) can change this handling. |
|
| 245 | + * |
|
| 246 | + * @var boolean |
|
| 247 | + */ |
|
| 248 | + public $_errorHandlingAPIBreak = false; |
|
| 249 | + |
|
| 250 | + // --- Public methods --- |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Constructor |
|
| 254 | + * |
|
| 255 | + * $options is an assoc. Available options are : |
|
| 256 | + * $options = array( |
|
| 257 | + * 'cacheDir' => directory where to put the cache files (string) , |
|
| 258 | + * 'caching' => enable / disable caching (boolean) , |
|
| 259 | + * 'lifeTime' => cache lifetime in seconds (int) , |
|
| 260 | + * 'fileLocking' => enable / disable fileLocking (boolean) , |
|
| 261 | + * 'writeControl' => enable / disable write control (boolean) , |
|
| 262 | + * 'readControl' => enable / disable read control (boolean) , |
|
| 263 | + * 'readControlType' => type of read control 'crc32', 'md5', 'strlen' (string) , |
|
| 264 | + * 'pearErrorMode' => pear error mode (when raiseError is called) (cf PEAR doc) (int) , |
|
| 265 | + * 'memoryCaching' => enable / disable memory caching (boolean) , |
|
| 266 | + * 'onlyMemoryCaching' => enable / disable only memory caching (boolean) , |
|
| 267 | + * 'memoryCachingLimit' => max nbr of records to store into memory caching (int) , |
|
| 268 | + * 'fileNameProtection' => enable / disable automatic file name protection (boolean) , |
|
| 269 | + * 'automaticSerialization' => enable / disable automatic serialization (boolean) , |
|
| 270 | + * 'automaticCleaningFactor' => distable / tune automatic cleaning process (int) , |
|
| 271 | + * 'hashedDirectoryLevel' => level of the hashed directory system (int) , |
|
| 272 | + * 'hashedDirectoryUmask' => umask for hashed directory structure (int) , |
|
| 273 | + * 'errorHandlingAPIBreak' => API break for better error handling ? (boolean) |
|
| 274 | + * ); |
|
| 275 | + * |
|
| 276 | + * @param array $options options |
|
| 277 | + * @access public |
|
| 278 | + */ |
|
| 279 | + public function __construct($options = array(null)) |
|
| 280 | + { |
|
| 281 | + foreach ($options as $key => $value) { |
|
| 282 | + $this->setOption($key, $value); |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Generic way to set a Cache_Lite option |
|
| 288 | + * |
|
| 289 | + * see Cache_Lite constructor for available options |
|
| 290 | + * |
|
| 291 | + * @var string $name name of the option |
|
| 292 | + * @var mixed $value value of the option |
|
| 293 | + * @access public |
|
| 294 | + */ |
|
| 295 | + public function setOption($name, $value) |
|
| 296 | + { |
|
| 297 | + $availableOptions = array( |
|
| 298 | + 'errorHandlingAPIBreak', |
|
| 299 | + 'hashedDirectoryUmask', |
|
| 300 | + 'hashedDirectoryLevel', |
|
| 301 | + 'automaticCleaningFactor', |
|
| 302 | + 'automaticSerialization', |
|
| 303 | + 'fileNameProtection', |
|
| 304 | + 'memoryCaching', |
|
| 305 | + 'onlyMemoryCaching', |
|
| 306 | + 'memoryCachingLimit', |
|
| 307 | + 'cacheDir', |
|
| 308 | + 'caching', |
|
| 309 | + 'lifeTime', |
|
| 310 | + 'fileLocking', |
|
| 311 | + 'writeControl', |
|
| 312 | + 'readControl', |
|
| 313 | + 'readControlType', |
|
| 314 | + 'pearErrorMode' |
|
| 315 | + ); |
|
| 316 | + if (in_array($name, $availableOptions)) { |
|
| 317 | + $property = '_' . $name; |
|
| 318 | + $this->$property = $value; |
|
| 319 | + } |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Test if a cache is available and (if yes) return it |
|
| 324 | + * |
|
| 325 | + * @param string $id cache id |
|
| 326 | + * @param string $group name of the cache group |
|
| 327 | + * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested |
|
| 328 | + * @return string data of the cache (else : false) |
|
| 329 | + * @access public |
|
| 330 | + */ |
|
| 331 | + public function get($id, $group = 'default', $doNotTestCacheValidity = false) |
|
| 332 | + { |
|
| 333 | + $this->_id = $id; |
|
| 334 | + $this->_group = $group; |
|
| 335 | + $data = false; |
|
| 336 | + if ($this->_caching) { |
|
| 337 | + $this->_setRefreshTime(); |
|
| 338 | + $this->_setFileName($id, $group); |
|
| 339 | + clearstatcache(); |
|
| 340 | + if ($this->_memoryCaching) { |
|
| 341 | + if (isset($this->_memoryCachingArray[$this->_file])) { |
|
| 342 | + if ($this->_automaticSerialization) { |
|
| 343 | + return unserialize($this->_memoryCachingArray[$this->_file]); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + return $this->_memoryCachingArray[$this->_file]; |
|
| 347 | + } |
|
| 348 | + if ($this->_onlyMemoryCaching) { |
|
| 349 | + return false; |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + if ($doNotTestCacheValidity || is_null($this->_refreshTime)) { |
|
| 353 | + if (file_exists($this->_file)) { |
|
| 354 | + $data = $this->_read(); |
|
| 355 | + } |
|
| 356 | + } else { |
|
| 357 | + if (file_exists($this->_file) && (@filemtime($this->_file) > $this->_refreshTime)) { |
|
| 358 | + $data = $this->_read(); |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + if ($data && $this->_memoryCaching) { |
|
| 362 | + $this->_memoryCacheAdd($data); |
|
| 363 | + } |
|
| 364 | + if ($this->_automaticSerialization && is_string($data)) { |
|
| 365 | + $data = unserialize($data); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + return $data; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return false; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Save some data in a cache file |
|
| 376 | + * |
|
| 377 | + * @param string $data data to put in cache (can be another type than strings if automaticSerialization is on) |
|
| 378 | + * @param string $id cache id |
|
| 379 | + * @param string $group name of the cache group |
|
| 380 | + * @return boolean true if no problem (else : false or a PEAR_Error object) |
|
| 381 | + * @access public |
|
| 382 | + */ |
|
| 383 | + public function save($data, $id = null, $group = 'default') |
|
| 384 | + { |
|
| 385 | + if ($this->_caching) { |
|
| 386 | + if ($this->_automaticSerialization) { |
|
| 387 | + $data = serialize($data); |
|
| 388 | + } |
|
| 389 | + if (isset($id)) { |
|
| 390 | + $this->_setFileName($id, $group); |
|
| 391 | + } |
|
| 392 | + if ($this->_memoryCaching) { |
|
| 393 | + $this->_memoryCacheAdd($data); |
|
| 394 | + if ($this->_onlyMemoryCaching) { |
|
| 395 | + return true; |
|
| 396 | + } |
|
| 397 | + } |
|
| 398 | + if ($this->_automaticCleaningFactor > 0 && ($this->_automaticCleaningFactor == 1 || mt_rand(1, $this->_automaticCleaningFactor) == 1)) { |
|
| 399 | + $this->clean(false, 'old'); |
|
| 400 | + } |
|
| 401 | + if ($this->_writeControl) { |
|
| 402 | + $res = $this->_writeAndControl($data); |
|
| 403 | + if (is_bool($res)) { |
|
| 404 | + if ($res) { |
|
| 405 | + return true; |
|
| 406 | + } |
|
| 407 | + // if $res if false, we need to invalidate the cache |
|
| 408 | + @touch($this->_file, time() - 2 * abs($this->_lifeTime)); |
|
| 409 | + |
|
| 410 | + return false; |
|
| 411 | + } |
|
| 412 | + } else { |
|
| 413 | + $res = $this->_write($data); |
|
| 414 | + } |
|
| 415 | + if (is_object($res)) { |
|
| 416 | + // $res is a PEAR_Error object |
|
| 417 | + if (!$this->_errorHandlingAPIBreak) { |
|
| 418 | + return false; // we return false (old API) |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + return $res; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + return false; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * Remove a cache file |
|
| 430 | + * |
|
| 431 | + * @param string $id cache id |
|
| 432 | + * @param string $group name of the cache group |
|
| 433 | + * @param boolean $checkbeforeunlink check if file exists before removing it |
|
| 434 | + * @return boolean true if no problem |
|
| 435 | + * @access public |
|
| 436 | + */ |
|
| 437 | + public function remove($id, $group = 'default', $checkbeforeunlink = false) |
|
| 438 | + { |
|
| 439 | + $this->_setFileName($id, $group); |
|
| 440 | + if ($this->_memoryCaching) { |
|
| 441 | + if (isset($this->_memoryCachingArray[$this->_file])) { |
|
| 442 | + unset($this->_memoryCachingArray[$this->_file]); |
|
| 443 | + --$this->_memoryCachingCounter; |
|
| 444 | + } |
|
| 445 | + if ($this->_onlyMemoryCaching) { |
|
| 446 | + return true; |
|
| 447 | + } |
|
| 448 | + } |
|
| 449 | + if ($checkbeforeunlink) { |
|
| 450 | + if (!file_exists($this->_file)) { |
|
| 451 | + return true; |
|
| 452 | + } |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + return $this->_unlink($this->_file); |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * Clean the cache |
|
| 460 | + * |
|
| 461 | + * if no group is specified all cache files will be destroyed |
|
| 462 | + * else only cache files of the specified group will be destroyed |
|
| 463 | + * |
|
| 464 | + * @param bool|string $group name of the cache group |
|
| 465 | + * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup', |
|
| 466 | + * 'callback_myFunction' |
|
| 467 | + * @return bool true if no problem |
|
| 468 | + * @access public |
|
| 469 | + */ |
|
| 470 | + public function clean($group = false, $mode = 'ingroup') |
|
| 471 | + { |
|
| 472 | + return $this->_cleanDir($this->_cacheDir, $group, $mode); |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * Set to debug mode |
|
| 477 | + * |
|
| 478 | + * When an error is found, the script will stop and the message will be displayed |
|
| 479 | + * (in debug mode only). |
|
| 480 | + * |
|
| 481 | + * @access public |
|
| 482 | + */ |
|
| 483 | + public function setToDebug() |
|
| 484 | + { |
|
| 485 | + $this->setOption('pearErrorMode', REFERENCES_CACHE_LITE_ERROR_DIE); |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * Set a new life time |
|
| 490 | + * |
|
| 491 | + * @param int $newLifeTime new life time (in seconds) |
|
| 492 | + * @access public |
|
| 493 | + */ |
|
| 494 | + public function setLifeTime($newLifeTime) |
|
| 495 | + { |
|
| 496 | + $this->_lifeTime = $newLifeTime; |
|
| 497 | + $this->_setRefreshTime(); |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + /** |
|
| 501 | + * Save the state of the caching memory array into a cache file cache |
|
| 502 | + * |
|
| 503 | + * @param string $id cache id |
|
| 504 | + * @param string $group name of the cache group |
|
| 505 | + * @access public |
|
| 506 | + */ |
|
| 507 | + public function saveMemoryCachingState($id, $group = 'default') |
|
| 508 | + { |
|
| 509 | + if ($this->_caching) { |
|
| 510 | + $array = array( |
|
| 511 | + 'counter' => $this->_memoryCachingCounter, |
|
| 512 | + 'array' => $this->_memoryCachingArray |
|
| 513 | + ); |
|
| 514 | + $data = serialize($array); |
|
| 515 | + $this->save($data, $id, $group); |
|
| 516 | + } |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * Load the state of the caching memory array from a given cache file cache |
|
| 521 | + * |
|
| 522 | + * @param string $id cache id |
|
| 523 | + * @param string $group name of the cache group |
|
| 524 | + * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested |
|
| 525 | + * @access public |
|
| 526 | + */ |
|
| 527 | + public function getMemoryCachingState($id, $group = 'default', $doNotTestCacheValidity = false) |
|
| 528 | + { |
|
| 529 | + if ($this->_caching) { |
|
| 530 | + if ($data = $this->get($id, $group, $doNotTestCacheValidity)) { |
|
| 531 | + $array = unserialize($data); |
|
| 532 | + $this->_memoryCachingCounter = $array['counter']; |
|
| 533 | + $this->_memoryCachingArray = $array['array']; |
|
| 534 | + } |
|
| 535 | + } |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Return the cache last modification time |
|
| 540 | + * |
|
| 541 | + * BE CAREFUL : THIS METHOD IS FOR HACKING ONLY ! |
|
| 542 | + * |
|
| 543 | + * @return int last modification time |
|
| 544 | + */ |
|
| 545 | + public function lastModified() |
|
| 546 | + { |
|
| 547 | + return @filemtime($this->_file); |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * Trigger a PEAR error |
|
| 552 | + * |
|
| 553 | + * To improve performances, the PEAR.php file is included dynamically. |
|
| 554 | + * The file is so included only when an error is triggered. So, in most |
|
| 555 | + * cases, the file isn't included and perfs are much better. |
|
| 556 | + * |
|
| 557 | + * @param string $msg error message |
|
| 558 | + * @param int $code error code |
|
| 559 | + * @access public |
|
| 560 | + * @return object |
|
| 561 | + */ |
|
| 562 | + public function raiseError($msg, $code) |
|
| 563 | + { |
|
| 564 | + include_once('PEAR.php'); |
|
| 565 | + |
|
| 566 | + return references_PEAR::raiseError($msg, $code, $this->_pearErrorMode); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Extend the life of a valid cache file |
|
| 571 | + * |
|
| 572 | + * see http://pear.php.net/bugs/bug.php?id=6681 |
|
| 573 | + * |
|
| 574 | + * @access public |
|
| 575 | + */ |
|
| 576 | + public function extendLife() |
|
| 577 | + { |
|
| 578 | + @touch($this->_file); |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + // --- Private methods --- |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * Compute & set the refresh time |
|
| 585 | + * |
|
| 586 | + * @access private |
|
| 587 | + */ |
|
| 588 | + public function _setRefreshTime() |
|
| 589 | + { |
|
| 590 | + if (is_null($this->_lifeTime)) { |
|
| 591 | + $this->_refreshTime = null; |
|
| 592 | + } else { |
|
| 593 | + $this->_refreshTime = time() - $this->_lifeTime; |
|
| 594 | + } |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * Remove a file |
|
| 599 | + * |
|
| 600 | + * @param string $file complete file path and name |
|
| 601 | + * @return boolean true if no problem |
|
| 602 | + * @access private |
|
| 603 | + */ |
|
| 604 | + public function _unlink($file) |
|
| 605 | + { |
|
| 606 | + if (!@unlink($file)) { |
|
| 607 | + return $this->raiseError('Cache_Lite : Unable to remove cache !', -3); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + return true; |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + /** |
|
| 614 | + * Recursive function for cleaning cache file in the given directory |
|
| 615 | + * |
|
| 616 | + * @param string $dir directory complete path (with a trailing slash) |
|
| 617 | + * @param bool|string $group name of the cache group |
|
| 618 | + * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup', |
|
| 619 | + * 'callback_myFunction' |
|
| 620 | + * @return bool true if no problem |
|
| 621 | + * @access private |
|
| 622 | + */ |
|
| 623 | + public function _cleanDir($dir, $group = false, $mode = 'ingroup') |
|
| 624 | + { |
|
| 625 | + if ($this->_fileNameProtection) { |
|
| 626 | + $motif = $group ? 'cache_' . md5($group) . '_' : 'cache_'; |
|
| 627 | + } else { |
|
| 628 | + $motif = $group ? 'cache_' . $group . '_' : 'cache_'; |
|
| 629 | + } |
|
| 630 | + if ($this->_memoryCaching) { |
|
| 631 | + foreach ($this->_memoryCachingArray as $key => $v) { |
|
| 632 | + if (strpos($key, $motif) !== false) { |
|
| 633 | + unset($this->_memoryCachingArray[$key]); |
|
| 634 | + --$this->_memoryCachingCounter; |
|
| 635 | + } |
|
| 636 | + } |
|
| 637 | + if ($this->_onlyMemoryCaching) { |
|
| 638 | + return true; |
|
| 639 | + } |
|
| 640 | + } |
|
| 641 | + if (!($dh = opendir($dir))) { |
|
| 642 | + return $this->raiseError('Cache_Lite : Unable to open cache directory !', -4); |
|
| 643 | + } |
|
| 644 | + $result = true; |
|
| 645 | + while ($file = readdir($dh)) { |
|
| 646 | + if (($file !== '.') && ($file !== '..')) { |
|
| 647 | + if (substr($file, 0, 6) === 'cache_') { |
|
| 648 | + $file2 = $dir . $file; |
|
| 649 | + if (is_file($file2)) { |
|
| 650 | + switch (substr($mode, 0, 9)) { |
|
| 651 | + case 'old': |
|
| 652 | + // files older than lifeTime get deleted from cache |
|
| 653 | + if (!is_null($this->_lifeTime)) { |
|
| 654 | + if ((time() - @filemtime($file2)) > $this->_lifeTime) { |
|
| 655 | + $result = ($result and $this->_unlink($file2)); |
|
| 656 | + } |
|
| 657 | + } |
|
| 658 | + break; |
|
| 659 | + case 'notingrou': |
|
| 660 | + if (strpos($file2, $motif) === false) { |
|
| 661 | + $result = ($result and $this->_unlink($file2)); |
|
| 662 | + } |
|
| 663 | + break; |
|
| 664 | + case 'callback_': |
|
| 665 | + $func = substr($mode, 9, strlen($mode) - 9); |
|
| 666 | + if ($func($file2, $group)) { |
|
| 667 | + $result = ($result and $this->_unlink($file2)); |
|
| 668 | + } |
|
| 669 | + break; |
|
| 670 | + case 'ingroup': |
|
| 671 | + default: |
|
| 672 | + if (strpos($file2, $motif) !== false) { |
|
| 673 | + $result = ($result and $this->_unlink($file2)); |
|
| 674 | + } |
|
| 675 | + break; |
|
| 676 | + } |
|
| 677 | + } |
|
| 678 | + if (is_dir($file2) and ($this->_hashedDirectoryLevel > 0)) { |
|
| 679 | + $result = ($result and $this->_cleanDir($file2 . '/', $group, $mode)); |
|
| 680 | + } |
|
| 681 | + } |
|
| 682 | + } |
|
| 683 | + } |
|
| 684 | + |
|
| 685 | + return $result; |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + /** |
|
| 689 | + * Add some date in the memory caching array |
|
| 690 | + * |
|
| 691 | + * @param string $data data to cache |
|
| 692 | + * @access private |
|
| 693 | + */ |
|
| 694 | + public function _memoryCacheAdd($data) |
|
| 695 | + { |
|
| 696 | + $this->_memoryCachingArray[$this->_file] = $data; |
|
| 697 | + if ($this->_memoryCachingCounter >= $this->_memoryCachingLimit) { |
|
| 698 | + list($key,) = each($this->_memoryCachingArray); |
|
| 699 | + unset($this->_memoryCachingArray[$key]); |
|
| 700 | + } else { |
|
| 701 | + ++$this->_memoryCachingCounter; |
|
| 702 | + } |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * Make a file name (with path) |
|
| 707 | + * |
|
| 708 | + * @param string $id cache id |
|
| 709 | + * @param string $group name of the group |
|
| 710 | + * @access private |
|
| 711 | + */ |
|
| 712 | + public function _setFileName($id, $group) |
|
| 713 | + { |
|
| 714 | + if ($this->_fileNameProtection) { |
|
| 715 | + $suffix = 'cache_' . md5($group) . '_' . md5($id); |
|
| 716 | + } else { |
|
| 717 | + $suffix = 'cache_' . $group . '_' . $id; |
|
| 718 | + } |
|
| 719 | + $root = $this->_cacheDir; |
|
| 720 | + if ($this->_hashedDirectoryLevel > 0) { |
|
| 721 | + $hash = md5($suffix); |
|
| 722 | + for ($i = 0; $i < $this->_hashedDirectoryLevel; ++$i) { |
|
| 723 | + $root = $root . 'cache_' . substr($hash, 0, $i + 1) . '/'; |
|
| 724 | + } |
|
| 725 | + } |
|
| 726 | + $this->_fileName = $suffix; |
|
| 727 | + $this->_file = $root . $suffix; |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + /** |
|
| 731 | + * Read the cache file and return the content |
|
| 732 | + * |
|
| 733 | + * @return string content of the cache file (else : false or a PEAR_Error object) |
|
| 734 | + * @access private |
|
| 735 | + */ |
|
| 736 | + public function _read() |
|
| 737 | + { |
|
| 738 | + $fp = @fopen($this->_file, 'rb'); |
|
| 739 | + if ($this->_fileLocking) { |
|
| 740 | + @flock($fp, LOCK_SH); |
|
| 741 | + } |
|
| 742 | + if ($fp) { |
|
| 743 | + clearstatcache(); |
|
| 744 | + $length = @filesize($this->_file); |
|
| 745 | + // $mqr = get_magic_quotes_runtime(); |
|
| 746 | + // @set_magic_quotes_runtime(0); |
|
| 747 | + if ($this->_readControl) { |
|
| 748 | + $hashControl = @fread($fp, 32); |
|
| 749 | + $length = $length - 32; |
|
| 750 | + } |
|
| 751 | + if ($length) { |
|
| 752 | + $data = @fread($fp, $length); |
|
| 753 | + } else { |
|
| 754 | + $data = ''; |
|
| 755 | + } |
|
| 756 | + // @set_magic_quotes_runtime($mqr); |
|
| 757 | + if ($this->_fileLocking) { |
|
| 758 | + @flock($fp, LOCK_UN); |
|
| 759 | + } |
|
| 760 | + @fclose($fp); |
|
| 761 | + if ($this->_readControl) { |
|
| 762 | + $hashData = $this->_hash($data, $this->_readControlType); |
|
| 763 | + if ($hashData != $hashControl) { |
|
| 764 | + if (!is_null($this->_lifeTime)) { |
|
| 765 | + @touch($this->_file, time() - 2 * abs($this->_lifeTime)); |
|
| 766 | + } else { |
|
| 767 | + @unlink($this->_file); |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + return false; |
|
| 771 | + } |
|
| 772 | + } |
|
| 773 | + |
|
| 774 | + return $data; |
|
| 775 | + } |
|
| 776 | + |
|
| 777 | + return $this->raiseError('Cache_Lite : Unable to read cache !', -2); |
|
| 778 | + } |
|
| 779 | + |
|
| 780 | + /** |
|
| 781 | + * Write the given data in the cache file |
|
| 782 | + * |
|
| 783 | + * @param string $data data to put in cache |
|
| 784 | + * @return boolean true if ok (a PEAR_Error object else) |
|
| 785 | + * @access private |
|
| 786 | + */ |
|
| 787 | + public function _write($data) |
|
| 788 | + { |
|
| 789 | + if ($this->_hashedDirectoryLevel > 0) { |
|
| 790 | + $hash = md5($this->_fileName); |
|
| 791 | + $root = $this->_cacheDir; |
|
| 792 | + for ($i = 0; $i < $this->_hashedDirectoryLevel; ++$i) { |
|
| 793 | + $root = $root . 'cache_' . substr($hash, 0, $i + 1) . '/'; |
|
| 794 | + if (!(@is_dir($root))) { |
|
| 795 | + @mkdir($root, $this->_hashedDirectoryUmask); |
|
| 796 | + } |
|
| 797 | + } |
|
| 798 | + } |
|
| 799 | + $fp = @fopen($this->_file, 'wb'); |
|
| 800 | + if ($fp) { |
|
| 801 | + if ($this->_fileLocking) { |
|
| 802 | + @flock($fp, LOCK_EX); |
|
| 803 | + } |
|
| 804 | + if ($this->_readControl) { |
|
| 805 | + @fwrite($fp, $this->_hash($data, $this->_readControlType), 32); |
|
| 806 | + } |
|
| 807 | + // $mqr = get_magic_quotes_runtime(); |
|
| 808 | + // @set_magic_quotes_runtime(0); |
|
| 809 | + @fwrite($fp, $data); |
|
| 810 | + // @set_magic_quotes_runtime($mqr); |
|
| 811 | + if ($this->_fileLocking) { |
|
| 812 | + @flock($fp, LOCK_UN); |
|
| 813 | + } |
|
| 814 | + @fclose($fp); |
|
| 815 | + |
|
| 816 | + return true; |
|
| 817 | + } |
|
| 818 | + |
|
| 819 | + return $this->raiseError('Cache_Lite : Unable to write cache file : ' . $this->_file, -1); |
|
| 820 | + } |
|
| 821 | + |
|
| 822 | + /** |
|
| 823 | + * Write the given data in the cache file and control it just after to avoir corrupted cache entries |
|
| 824 | + * |
|
| 825 | + * @param string $data data to put in cache |
|
| 826 | + * @return boolean true if the test is ok (else : false or a PEAR_Error object) |
|
| 827 | + * @access private |
|
| 828 | + */ |
|
| 829 | + public function _writeAndControl($data) |
|
| 830 | + { |
|
| 831 | + $result = $this->_write($data); |
|
| 832 | + if (is_object($result)) { |
|
| 833 | + return $result; # We return the PEAR_Error object |
|
| 834 | + } |
|
| 835 | + $dataRead = $this->_read(); |
|
| 836 | + if (is_object($dataRead)) { |
|
| 837 | + return $dataRead; # We return the PEAR_Error object |
|
| 838 | + } |
|
| 839 | + if (is_bool($dataRead) && (!$dataRead)) { |
|
| 840 | + return false; |
|
| 841 | + } |
|
| 842 | + |
|
| 843 | + return ($dataRead == $data); |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + /** |
|
| 847 | + * Make a control key with the string containing datas |
|
| 848 | + * |
|
| 849 | + * @param string $data data |
|
| 850 | + * @param string $controlType type of control 'md5', 'crc32' or 'strlen' |
|
| 851 | + * @return string control key |
|
| 852 | + * @access private |
|
| 853 | + */ |
|
| 854 | + public function _hash($data, $controlType) |
|
| 855 | + { |
|
| 856 | + switch ($controlType) { |
|
| 857 | + case 'md5': |
|
| 858 | + return md5($data); |
|
| 859 | + case 'crc32': |
|
| 860 | + return sprintf('% 32d', crc32($data)); |
|
| 861 | + case 'strlen': |
|
| 862 | + return sprintf('% 32d', strlen($data)); |
|
| 863 | + default: |
|
| 864 | + return $this->raiseError('Unknown controlType ! (available values are only \'md5\', \'crc32\', \'strlen\')', -5); |
|
| 865 | + } |
|
| 866 | + } |
|
| 867 | 867 | } |