1
0
This repository has been archived on 2023-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
dtux__nextcloudconnector4do.../nconnector.php

165 lines
6.0 KiB
PHP
Raw Normal View History

#!/usr/bin/php
<?php
require("session_init.php");
require("config.inc.php");
require("log.php");
require("db.class.php");
2019-08-31 15:37:11 +02:00
function esp( $s )
{
$s = " $s ";
return $s;
}
2019-09-01 15:50:19 +02:00
2019-09-03 00:49:07 +02:00
function concat ($a, $b)
{
2019-09-03 00:53:50 +02:00
if ( !empty($a) && !empty($b) )
2019-09-03 00:49:07 +02:00
{
$r = $a . " " . $b;
}else
{
$r = $a . $b;
}
return $r;
}
$flag = 0;
2019-09-03 01:08:35 +02:00
$data = array("displayname"=>array("value"=>"","scope"=>"contacts","verified"=>"0"),"address"=>array("value"=>"[adresse]","scope"=>"private","verified"=>"0"),"website"=>array("value"=>"","scope"=>"private","verified"=>"0"),"email"=>array("value"=>"","scope"=>"contacts","verified"=>"1"),"avatar"=>array("scope"=>"contacts","verified"=>"0"),"phone"=>array("value"=>"","scope"=>"private","verified"=>"0"),"twitter"=>array("value"=>"","scope"=>"private","verified"=>"0"));
2019-09-03 00:49:07 +02:00
2019-08-31 12:41:48 +02:00
$doldb = new db("dolibarr");
$ncdb = new db("nextcloud");
2019-09-03 00:57:47 +02:00
$doldb->query("SELECT login, firstname, lastname, pass_crypted, fk_adherent_type, address, email, phone_perso, phone_mobile, zip, town, fk_user_valid, datefin FROM llx_adherent");
2019-09-03 00:57:47 +02:00
//print_r ($ncdb);
2019-08-31 17:37:23 +02:00
while ($user = $doldb->result->fetch_assoc())
2019-08-31 16:13:31 +02:00
{
2019-09-03 02:04:45 +02:00
//$flag+=1;
2019-11-30 00:55:06 +01:00
$insert = false;
2019-11-30 00:30:43 +01:00
if ($user["fk_user_valid"] == null) $user["fk_user_valid"] = 0;
2019-08-31 18:00:13 +02:00
$ncdb->query("SELECT uid FROM oc_accounts WHERE uid='" . $user["login"] . "'");
2019-08-31 18:03:21 +02:00
echo "login:" . ($user["login"]) . EOL;
2019-08-31 17:54:17 +02:00
if ( $ncdb->result->num_rows != 0 )
2019-08-31 17:37:23 +02:00
{
// L'utilisateur existe déjà, on ne le crée pas, mais on vérifie sa validité et si il est à jour de cotisation.
2019-08-31 17:46:12 +02:00
$ncuser = $ncdb->result->fetch_assoc();
2019-12-01 01:33:33 +01:00
echo "uid:" . $ncuser["uid"] . " existe" . EOL;
2019-11-30 00:15:37 +01:00
$ncdb->query("SELECT configvalue FROM oc_preferences WHERE userid='" . $user["login"] . "' AND appid='core' AND configkey='enabled'");
2019-11-30 00:04:49 +01:00
if ( $ncdb->result->num_rows != 0 )
{
// le validité de l'utilisateur est définie
2019-12-18 22:30:26 +01:00
echo "utilisateur valide";
2019-11-30 01:04:30 +01:00
$r = $ncdb->result->fetch_assoc();
2019-11-30 00:17:53 +01:00
$status = $r["configvalue"] ;
2019-11-30 00:04:49 +01:00
}else
{
// pas de définition de la validité de l'utilisateur => par défaut utilisateur valide
2019-12-18 22:30:26 +01:00
echo "utilisateur valide par défaut";
$insert = true; // insertion de la validité dans la table préférence
2019-12-01 11:16:26 +01:00
$status = 'true';
2019-11-30 00:04:49 +01:00
}
2019-12-18 22:56:57 +01:00
$fk_user_valid = ($user["fk_user_valid"]==0?"false":"true");
if ( $fk_user_valid != $status )
2019-11-30 00:04:49 +01:00
{
// différence entre nexcloud et dolibarr
2019-12-18 22:56:57 +01:00
echo "\ndifférent\n##############################\n" . $fk_user_valid . "!=" . $status . "\n";
write_status( $insert, $user["login"], $fk_user_valid, $ncdb );
}
// vérification si l'utilisateur est à jour de cotisation
2019-12-18 22:35:57 +01:00
echo "datefin =>" . $user['datefin'];
if( !empty($user['datefin']) )
{
2019-12-01 01:41:57 +01:00
$datefin = DateTime::createFromFormat("Y-m-d H:i:s", $user['datefin']);
$interval = new DateInterval('P0000Y02M00D');
$datefin->add($interval);
print_r($datefin);
if ( $datefin->getTimestamp() <= time() )
{
2019-12-01 11:16:26 +01:00
write_status( $insert, $user['login'] , "false", $ncdb );
2019-12-01 01:41:57 +01:00
}elseif ( $status == 0 )
{
// ré active l'utlisateur désactivé à jour de cotisation.
2019-12-01 11:16:26 +01:00
write_status( $insert, $user['login'] , "true", $ncdb );
2019-12-01 01:41:57 +01:00
}
2019-11-30 00:04:49 +01:00
}
2019-08-31 18:06:39 +02:00
}else
{
2019-08-31 23:59:24 +02:00
//echo $user["login"] . " n'existe pas";
2019-08-31 19:03:27 +02:00
if ( empty($user["phone_mobile"]) )
{
2019-09-03 00:49:07 +02:00
$data["phone"]["value"] = $user["phone_perso"];
2019-08-31 19:03:27 +02:00
}else
{
2019-09-03 00:49:07 +02:00
$data["phone"]["value"] = $user["phone_mobile"];
2019-08-31 19:03:27 +02:00
}
2019-09-02 23:22:45 +02:00
2019-09-03 02:34:21 +02:00
$data["displayname"]["value"] = addslashes(concat(utf8_encode($user["firstname"]), utf8_encode($user["lastname"])));
2019-09-03 01:35:53 +02:00
$address = concat(utf8_encode($user['address']), $user["zip"]);
2019-09-03 02:37:37 +02:00
$data["address"]["value"] = (concat ($address, utf8_encode($user["town"])));
$data["email"]["value"] = "'" . $user["email"] . "'";
2019-09-03 01:26:26 +02:00
print_r($data);
2019-11-30 00:55:06 +01:00
if ( ($account_data = json_encode($data)) == false )
2019-09-03 01:26:26 +02:00
{
echo json_last_error();
2019-09-03 01:27:43 +02:00
exit (0);
2019-09-03 01:26:26 +02:00
}
2019-09-03 00:49:07 +02:00
2019-09-03 02:39:37 +02:00
$oc_account_query = 'INSERT INTO oc_accounts (uid, data) VALUES ("' . $user["login"] . '","' . addslashes($account_data) .'")';
2019-09-03 02:34:21 +02:00
$oc_users_query = 'INSERT INTO oc_users (uid, displayname, password, uid_lower) VALUES ("' . $user["login"] . '","' . $user["firstname"] . ' ' . $user["lastname"] . '","1|' . $user["pass_crypted"] . '","' . strtolower($user["login"]) . '")';
2019-08-31 23:59:24 +02:00
if ($user["fk_adherent_type"] == 2)
{
$gid="membres";
}else if ($user["fk_adherent_type"] == 3)
{
$gid="contributeurs";
}
2019-09-01 00:09:43 +02:00
$oc_group_user_query = "INSERT INTO oc_group_user(uid, gid) VALUES ('" . $user["login"] . "','" . $gid . "')";
2019-09-01 00:57:33 +02:00
2019-09-02 22:57:35 +02:00
$oc_preferences_query_quota = "INSERT INTO oc_preferences(userid, appid, configkey, configvalue) VALUES ('" . $user["login"] . "', 'files', 'quota', '10 GB')";
$oc_preferences_query_email = "INSERT INTO oc_preferences(userid, appid, configkey, configvalue) VALUES ('" . $user["login"] . "', 'settings', 'email', '" . $user["email"] . "')";
2019-09-02 22:31:48 +02:00
echo $account_data . EOL . $oc_account_query . EOL . $oc_users_query .EOL . $oc_group_user_query . EOL . $oc_preferences_query_quota . EOL . $oc_preferences_query_email . EOL;
2019-09-03 01:38:20 +02:00
2019-11-06 19:03:38 +01:00
if ($ncdb->query($oc_account_query))
2019-09-01 00:33:53 +02:00
{
2019-09-01 15:48:24 +02:00
echo "Echec lors de la requete : (" . $ncdb->connect->errno . ") " . $ncdb->connect->error;
2019-09-01 00:33:53 +02:00
}
2019-09-03 01:51:01 +02:00
if (!$ncdb->query($oc_users_query))
2019-09-01 00:33:53 +02:00
{
2019-09-01 15:48:24 +02:00
echo "Echec lors de la requete : (" . $ncdb->connect->errno . ") " . $ncdb->connect->error;
2019-09-01 00:33:53 +02:00
}
2019-09-03 01:51:01 +02:00
if (!$ncdb->query($oc_group_user_query))
2019-09-01 00:33:53 +02:00
{
2019-09-01 15:48:24 +02:00
echo "Echec lors de la requete : (" . $ncdb->connect->errno . ") " . $ncdb->connect->error;
2019-09-01 00:33:53 +02:00
}
2019-09-03 01:51:01 +02:00
if (!$ncdb->query($oc_preferences_query_quota))
2019-09-02 22:31:48 +02:00
{
echo "Echec lors de la requete : (" . $ncdb->connect->errno . ") " . $ncdb->connect->error;
}
2019-09-03 01:51:01 +02:00
if (!$ncdb->query($oc_preferences_query_email))
2019-09-02 22:31:48 +02:00
{
echo "Echec lors de la requete : (" . $ncdb->connect->errno . ") " . $ncdb->connect->error;
2019-09-03 01:38:20 +02:00
}
2019-08-31 19:06:43 +02:00
}
2019-09-03 01:08:35 +02:00
//$ncdb->result->close();
2019-09-03 02:04:45 +02:00
//if ($flag > 12) exit;
2019-08-31 16:13:31 +02:00
}
2019-09-03 01:08:35 +02:00
//$doldb->result->close();
2019-12-18 22:30:26 +01:00
function write_status( $mode, $uid, $status, $db )
{
2019-12-18 22:30:26 +01:00
if ( $mode )
{
2019-12-01 11:16:26 +01:00
$query = "INSERT INTO oc_preferences (userid, appid, configkey, configvalue) VALUES ( '" . $uid . "', 'core', 'enabled', '" . $status . "' ) ";
}else
{
2019-12-01 11:16:26 +01:00
$query = "UPDATE oc_preferences SET configvalue = '" . $status ."' WHERE userid='" . $uid . "' AND appid='core' AND configkey='enabled'" ;
}
2019-12-01 01:50:13 +01:00
$db->query( $query );
echo $query;
}
?>