summaryrefslogtreecommitdiff
path: root/code/admin/utils.php
diff options
context:
space:
mode:
authorLudovic Pouzenc <lpouzenc@gmail.com>2013-10-29 18:42:08 +0100
committerLudovic Pouzenc <lpouzenc@gmail.com>2013-10-29 18:42:08 +0100
commitc98515883097467896a3f46b755c8cb892fe8961 (patch)
treebe2e4f5450e9bc9cf692e8dfac7c9780eaa7019b /code/admin/utils.php
downloadeditablesite-c98515883097467896a3f46b755c8cb892fe8961.tar.gz
editablesite-c98515883097467896a3f46b755c8cb892fe8961.tar.bz2
editablesite-c98515883097467896a3f46b755c8cb892fe8961.zip
Import initial avec une arbrescence éclatée
Diffstat (limited to 'code/admin/utils.php')
-rw-r--r--code/admin/utils.php179
1 files changed, 179 insertions, 0 deletions
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 @@
+<?php
+ function sanitize($arg_array, $arg_key, $replace_chars_re, $default_value) {
+ //FIXME : should check string type and strlen !
+ if ( ! array_key_exists($arg_key, $arg_array) ) return $default_value;
+ return preg_replace($replace_chars_re, '_', $arg_array[$arg_key]);
+ }
+
+ function sanitize_ini($ini_path, $array_entry_props) {
+ $array_ini = parse_ini_file($ini_path);
+ if ( is_array($array_ini) ) {
+ // Sanitize any existing ini entries. Destroy unwanted ones.
+ foreach ( $array_ini as $k => $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 "<pre>l10n file is '$base/$lang.utf8/LC_MESSAGES/$domain.mo'</pre>\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 "<ul>\n";
+ foreach ($page_tree as $k => $v) {
+ if ( is_numeric($k) ) {
+ // Leaf
+ if ( strlen($leaf_cb) > 0 ) {
+ echo '<li><a href="javascript:' . $leaf_cb . "('$path$v');" . '">' . $v . '</a></li>' . "\n";
+ } else {
+ echo "<li>$v</li>\n";
+ }
+ } else {
+ // Node
+ // Increment $itemid last component
+ // "1-1-2-1" will come "1-1-2-2"
+ $tmp = explode('-', $itemid);
+ array_push($tmp, array_pop($tmp) + 1);
+ $itemid = implode('-', $tmp);
+ $tmp=null;
+ /// Write the node
+ echo "<li>\n";
+ echo '<input type="checkbox" checked="checked" id="' . $itemid . '">' . "\n";
+ echo '<label for="' . $itemid . '">';
+ if ( strlen($node_cb) > 0 ) {
+ echo '<a href="javascript:' . $node_cb . "('$path$k');" . '">' . $k . '</a>';
+ } else {
+ echo $k;
+ }
+ echo "</label>\n";
+ // Recursively build the tree below the node
+ php_array_to_tree($v, $node_cb, $leaf_cb, $path.$k.'/', $itemid."-0");
+ echo "</li>\n";
+ }
+ }
+ echo "</ul>\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;
+ }
+