<?php
// :dev:tools [[:dev:tools.php]]
if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/');
global $_SESSION;
// =============================
// =============================
class cSession {
// =============================
static protected $sObj = null;
function __construct() {
self::$sObj = this;
@session_start();
if (!isset($_SESSION['cSession'])) $_SESSION['cSession']=array();
// echo "===</br>\n"; print_r($_SESSION); echo "</br>===</br>\n";
}
function __destruct() { self::$sObj = null; }
static function start() {
if ( self::$sObj==null ) { self::$sObj = new self; }
return self::$sObj;
}
function & get( $name ) {
$se = & $_SESSION['cSession'];
if ( isset( $se[$name] )) { return $se[$name]; }
return null;
}
function set( $name, & $var ) {
$se = & $_SESSION['cSession'];
if ($var === null) { unset($se[$name]); return; }
$se[$name] = $var;
}
} // cSession
// cSession::start();
// $v = & $se->get( 'my_v' );
// if ($v === null) { $v = 0; $se->set( 'my_v', $v ); }
// echo "\n\n v=". $v++ . "\n\n";
function getMySession() {
$my_session_id=$_COOKIE['PHPSESSID'];
if ($my_session_id == null) $my_session_id = $_COOKIE['DokuWiki'];
$confpath = $_SERVER['DOCUMENT_ROOT']."/dokuwiki/files/".$my_session_id."_conf.json";
// echo "conpath=". htmlentities($confpath, ENT_SUBSTITUTE, "UTF-8");
$mysession = array();
if (file_exists($confpath)) {
$fp=fopen($confpath, "r"); if ($fp !==false) { $js = fread($fp, 4096); $mysession=json_decode($js, true); fclose($fp); }
}
// print_r($mysession);
return $mysession;
}
function setMySession( $mysession ) {
$my_session_id=$_COOKIE['PHPSESSID'];
if ($my_session_id == null) $my_session_id = $_COOKIE['DokuWiki'];
$confpath = $_SERVER['DOCUMENT_ROOT']."/dokuwiki/files/".$my_session_id."_conf.json";
$fp=fopen($confpath, "w"); fwrite($fp, json_encode( $mysession, JSON_FORCE_OBJECT )); fclose($fp);
}
function readMySessionFromGet( & $mysession ) {
foreach( $_GET as $k => $v ) { if ( strpos($k, 'conf_') === 0 ) { $mysession[$k] = $v; } }
}
function _echo( $s ) {
global $echo_str;
$echo_str .= htmlspecialchars($s) . "</br>\n";
}
function printModal($id, $title, $msg) {
echo '<div class="bs-wrap bs-wrap-modal modal" id="'.$id.'" role="dialog" tabindex="-1" data-labelledby="'.$title.'">';
echo ' <div class="bs-wrap modal-dialog modal-" role="document">';
echo ' <div class="bs-wrap modal-content">';
echo ' <div class="bs-wrap modal-header">';
echo ' <button id="'.$id.'_btnclose" type="button" class="close btn btn-default" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>';
echo ' <h4 id="'.$id.'_title" class="bs-wrap modal-title">'.$title.'</h4>';
echo ' </div>';
echo ' <div id="'.$id.'_body" class="bs-wrap modal-body">';
echo ' '.$msg;
echo ' </div>';
echo ' </div>';
echo ' </div>';
echo '</div>';
}
function execNoWaitS($cmd) {
$cmd = escapeshellcmd ( $cmd );
$cmd = 'nohup '.$cmd.' > /dev/null 2>&1 & echo $!;'; // do not wait for the end of the cmd :
$r = exec($cmd);
return $r;
}
function execNoWait($cmd) {
$cmd = escapeshellcmd ( $cmd );
$cmd .= ' > /dev/null 2>&1 & echo $!;'; // do not wait for the end of the cmd :
// $command = 'yourcommand' . ' > /dev/null 2>&1 & echo $!; ';
// $pid = exec($command, $output);
$r = exec($cmd);
return $r;
}
function execWait($cmd, &$output = NULL, &$retval = NULL) {
$cmd = escapeshellcmd ( $cmd );
$r = exec($cmd, $output, $retval);
if ($retval !== NULL) { echo "retval=".$retval."</br>\n"; }
if ($output !== NULL) { foreach ($output as $k => $v) { echo ' *'.$k.' -- '.$v."</br>\n"; } }
return $r;
}
function getDokuID() {
global $ID;
return $ID;
}
function getDokuScript() {
return $_SERVER['SCRIPT_NAME'];
}
function isSecure() {
return
(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| $_SERVER['SERVER_PORT'] == 443;
}
function getUrl() {
$script = $_SERVER['SCRIPT_NAME'];
$url = ''; // $_SERVER['HTTP_REFERER'];
$http = 'http';
if (isSecure()) $http = 'https';
if ($url == '') { $url = $http.'://'.$_SERVER['HTTP_HOST'].'/'; }
if (strrpos($url, '/') == strlen($url)-1) { $url = substr($url, 0, -1); }
$i = strpos($url, $script); if ($i !== false) { $url = substr($url, 0, $i); }
return $url;
}
function add_log($s) {
$path = DOKU_INC.'dev/log.txt';
$f = fopen($path, 'a');
if ($f === false) { echo "error fopen ".$path."\n\n"; }
else {
$r = fwrite($f, $s."\n");
fclose($f);
}
}
function readPagesFile($fname) {
$path = DOKU_INC.'data/pages/'.$fname;
$excl = '';
$f = fopen($path, 'r');
if ($f === false) { echo "error fopen \n\n"; }
else {
$excl = fread($f, filesize($path));
fclose($f);
}
if ($excl === false) return '';
return $excl;
}
function writePagesFile($fname, $s) {
$path = DOKU_INC.'data/pages/'.$fname;
$f = fopen($path, 'w');
if ($f === false) { echo "error fopen \n\n"; }
else {
$r = fwrite($f, $s);
fclose($f);
}
}
function printFormUpload ($transfid, $maxsize) {
$action = $_SERVER['SCRIPT_NAME'] .'?id='.getDokuID();
// $action = getUrl().'/dokuwiki/dev/importaction.php';
// echo 'action = '. htmlspecialchars($action).'</br>';
echo '<div class="form-group">'; echo '<form action="'.$action.'" method="post" enctype="multipart/form-data">'; echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />'; echo '<input type="hidden" name="page" value="'.getDokuID().'" />'; echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.$maxsize.'" />'; echo '<input type="file" name="'.$transfid.'" class="edit form-control" />'; echo '<input type="submit" />'; echo "</form></div></br>\n"; }
function getUploadFile ($transfid, $maxsize) {
if (!isset($_FILES[$transfid])) { return false; }
$f = $_FILES[$transfid];
$destpath = $_SERVER['DOCUMENT_ROOT']."/dokuwiki/files/loadfile.txt";
echo "=== destination =[".$destpath."]</br>\n";
if (file_exists($destpath)) { unlink ($destpath); }
if ($f["error"] != UPLOAD_ERR_OK) {
echo "Erreur ".$f["error"]." lors du transfert" ;
return false;
}
$res = move_uploaded_file($f["tmp_name"], $destpath);
if ($res === true) {
echo "Transfert réussi du fichier [".$f['name']."]" ;
return $destpath;
}
echo "Echec lors du mv </br>";
return false;
}
?>