From c98515883097467896a3f46b755c8cb892fe8961 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Tue, 29 Oct 2013 18:42:08 +0100 Subject: Import initial avec une arbrescence éclatée MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/admin/utils.php | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 code/admin/utils.php (limited to 'code/admin/utils.php') diff --git a/code/admin/utils.php b/code/admin/utils.php new file mode 100644 index 0000000..f02146a --- /dev/null +++ b/code/admin/utils.php @@ -0,0 +1,179 @@ + $v ) { + if ( array_key_exists($k, $array_entry_props) + && array_key_exists('replace_chars_re', $array_entry_props[$k]) + && array_key_exists('default_value', $array_entry_props[$k]) + ) { + $array_ini[$k] = sanitize($array_ini, $k, + $array_entry_props[$k]['replace_chars_re'], + $array_entry_props[$k]['default_value'] ); + } else { + unset($array_ini[$k]); + } + } + // Set default value for all missing ini entries (if default value exists) + foreach ( $array_entry_props as $k => $v ) { + if ( !array_key_exists($k, $array_ini) && array_key_exists('default_value', $array_entry_props[$k]) ) { + $array_ini[$k] = $array_entry_props[$k]['default_value']; + } + } + } + return $array_ini; + } + + function load_ini_site_conf($ini_path) { + $sanitize_site_conf = array( + 'site_admin_lang' => array( 'replace_chars_re' => '/[^a-zA-Z\/\_-]+/', 'default_value' => 'C' ), + 'site_default_page' => array( 'replace_chars_re' => '/[^a-z0-9\/]+/', 'default_value' => 'en/index' ), + ); + return sanitize_ini($ini_path, $sanitize_site_conf); + } + + function load_ini_page_props($page) { + $sanitize_page_props = array( + //FIXME : title regex : all but html special chars ? + 'page_title' => array( 'replace_chars_re' => '/[^\w !_,.-]+/', 'default_value' => '(missing title in props.ini)' ), + 'page_template' => array( 'replace_chars_re' => '/[^a-z0-9]+/', 'default_value' => 'default' ), + 'page_layout' => array( 'replace_chars_re' => '/[^a-z0-9]+/', 'default_value' => 'article' ), + 'page_description' => array( 'replace_chars_re' => '/[^\w !_,.-]+/', 'default_value' => '(missing description in props.ini)' ), + 'page_keywords' => array( 'replace_chars_re' => '/[^\w !_,.-]+/', 'default_value' => '(missing keywords in props.ini)' ), + ); + $ini_path="content/$page/props.ini"; + return sanitize_ini($ini_path, $sanitize_page_props); + } + + function l10n_init($lang) { + setlocale(LC_MESSAGES, "$lang.utf8"); + $base = bindtextdomain('editablesite', './locale'); + $domain = textdomain('editablesite'); + bind_textdomain_codeset('editablesite', 'UTF-8'); + //echo "
l10n file is '$base/$lang.utf8/LC_MESSAGES/$domain.mo'
\n"; + } + + function need_auth() { + session_start(); + if ( ! array_key_exists('auth_user', $_SESSION) || $_SESSION['auth_user'] !== TRUE ) { + $_SESSION['auth_return'] = $_SERVER['REQUEST_URI']; + header('Location: auth.php'); + exit(); + } + } + + function is_ress($kind, $path) { + switch ($kind) { + case 'page': return is_file($path.'/props.ini'); + case 'media': return substr($path, -4)=='.jpg' && is_file($path); + default : return FALSE; + } + } + + function strcmp_tree($a,$b) { + if (is_array($a) || is_array($b) ) return 0; + return strnatcasecmp($a,$b); + } + + function find_all($path, $kind) { + $result=array(); + if ( $handle = opendir($path) ) { + while (FALSE !== ($entry = readdir($handle))) { + if ( array_search($entry, array('.','..')) !== FALSE ) continue; + $childpath=$path.'/'.$entry; + if ( is_ress($kind, $childpath) ) $result[] = $entry; + else if ( is_dir($childpath) ) $result[$entry]=find_all($childpath,$kind); + } + closedir($handle); + } + uasort($result, 'strcmp_tree'); + return $result; + } + + function php_array_to_tree($page_tree, $node_cb, $leaf_cb='', $path='', $itemid="item-0") { + if ( ! is_array($page_tree) ) return; + echo "\n"; + } + + function safe_put_file($path, $content) { + //FIXME : if exists, then mktemp, put in it then rm and mv. Right preservation problems ? + if ($handle = fopen($path, 'w')) { + $res = fwrite($handle, $content); + fclose($handle); + } + } + + function _write_ini_file_r(&$content, $assoc_arr, $has_sections) + { + foreach ($assoc_arr as $key => $val) { + if (is_array($val)) { + if($has_sections) { + $content .= "[$key]\n"; + _write_ini_file_r($content, $val, false); + } else { + foreach($val as $iKey => $iVal) { + if (is_int($iKey)) + $content .= $key ."[] = $iVal\n"; + else + $content .= $key ."[$iKey] = $iVal\n"; + } + } + } else { + if ( preg_match('/^\w+$/',$val)===1 ) + $content .= "$key = $val\n"; + else + $content .= "$key = \"" . str_replace('"', '\"', $val) . "\"\n"; + } + } + } + + function write_ini_file($assoc_arr, $path, $has_sections) { + $res=FALSE; + $content = ''; + _write_ini_file_r($content, $assoc_arr, $has_sections); + if (is_string($content) && strlen($content) > 0) { + safe_put_file($path, $content); + } + + return $res; + } + -- cgit v1.2.3