SHARE
TWEET

Untitled

a guest Oct 16th, 2017 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var mysql = require('mysql2');
  2. var fs = require('fs');
  3.  
  4. var errors=[];
  5. var List=[];
  6. var INSERT=[];
  7.  
  8. // create the connection to database
  9. const connection = mysql.createConnection({
  10.   host: '127.0.0.1',
  11.   user: 'oc_admin',
  12.   database: 'cloudDB',
  13.   password: 'XXX'
  14. });
  15.  
  16.  
  17. function query(list,i,cb) {
  18.   if (!list[i]) return cb();
  19.   connection.query(
  20.     'SELECT * FROM `oc_ldap_user_mapping` where directory_uuid="'+list[i]+'"',
  21.     function(err, results, fields) {
  22.       if (results.length==0) {
  23.         errors.push(list[i]);
  24.         query(list,i+1,cb);
  25.         return;
  26.       };
  27.       List.push(results);
  28.       query(list,i+1,cb);
  29.     }
  30.   );
  31. };
  32.  
  33. function check(list,i,cb) {
  34.   if (!list[i]) return cb();
  35.   var cn=list[i][0].ldap_dn;
  36.   var name=list[i][0].owncloud_name;
  37.   var nomprenom=(cn.split('cn=')[1].split(',')[0].split('\\28')[0]);
  38.   nomprenom=nomprenom.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
  39.   var mail=(cn.split('cn=')[1].split(',')[0].split('\\28')[1].split('\\29')[0]);
  40.   connection.query(
  41.     'SELECT * FROM `oc_users` where uid="'+mail+'" or uid="'+name+'"',
  42.     function(err, results, fields) {
  43.       if (results.length==0) {
  44.           // Cet utilisateur n'a pas été crée dans la base
  45.           INSERT.push("INSERT INTO oc_users (uid,displayname,password) VALUES ('"+mail+"','"+nomprenom+"','-1')");
  46.           INSERT.push("UPDATE oc_ldap_user_mapping SET owncloud_name='"+mail+"' WHERE ldap_dn='"+cn+"'");
  47.       };
  48.       check(list,i+1,cb);
  49.     }
  50.   );
  51. };
  52.  
  53. function do_insert(insert,i,cb) {
  54.   if (!insert[i]) return cb();
  55.   connection.query(insert[i],function(err, results, fields) {
  56.     console.log('err: '+err );
  57.     console.log('response: '+results);
  58.     do_insert(insert,i+1,cb);
  59.   });
  60. };
  61.  
  62. fs.readdir('/data/',function(e,r) {
  63.   var files=[];
  64.   for (var i=0;i<r.length;i++) {
  65.     if ((r[i].indexOf('.ocdata')==-1) && (r[i].indexOf('.htaccess')==-1) && (r[i].indexOf('owncloud.log')==-1) && (r[i].indexOf('owncloud')==-1)) files.push(r[i]);
  66.   };
  67.   query(files,0,function() {
  68.     check(List,0,function() {
  69.  
  70.       do_insert(INSERT,0,function() {
  71.         console.log('ok');
  72.       });
  73.     });
  74.   });
  75. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top