developer/web/cloud.php

200 lines
9.9 KiB
PHP
Raw Normal View History

2013-08-28 22:47:04 +02:00
<?php
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
/**
* File: cloud.php.
* Author: Ulrich Block
* Date: 21.10.12
* Time: 10:24
* Contact: <ulrich.block@easy-wi.com>
*
* This file is part of Easy-WI.
*
* Easy-WI is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Easy-WI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Easy-WI. If not, see <http://www.gnu.org/licenses/>.
*
* Diese Datei ist Teil von Easy-WI.
*
* Easy-WI ist Freie Software: Sie koennen es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder spaeteren
* veroeffentlichten Version, weiterverbreiten und/oder modifizieren.
*
* Easy-WI wird in der Hoffnung, dass es nuetzlich sein wird, aber
* OHNE JEDE GEWAEHELEISTUNG, bereitgestellt; sogar ohne die implizite
* Gewaehrleistung der MARKTFAEHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License fuer weitere Details.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
*/
2013-10-16 19:56:54 +02:00
define('EASYWIDIR', dirname(__FILE__));
2013-08-28 22:47:04 +02:00
if (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
2013-10-16 19:56:54 +02:00
$timelimit = (isset($_GET['timeout']) and is_numeric($_GET['timeout'])) ? (int) $_GET['timeout'] : ini_get('max_execution_time') - 10;
2013-08-28 22:47:04 +02:00
} else {
2013-10-16 19:56:54 +02:00
$timelimit = 600;
2013-08-28 22:47:04 +02:00
}
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
set_time_limit($timelimit);
2013-10-16 19:56:54 +02:00
if (!isset($ip) or $_SERVER['SERVER_ADDR'] == $ip) {
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
function printText ($text) {
echo $text."\r\n";
}
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
function getParam ($v) {
global $value;
2013-10-16 19:56:54 +02:00
return (isset($value->$v)) ? $value->$v : '';
2013-08-28 22:47:04 +02:00
}
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
printText('Cloud jobs started');
2013-10-16 19:56:54 +02:00
include(EASYWIDIR . '/stuff/vorlage.php');
include(EASYWIDIR . '/stuff/functions.php');
include(EASYWIDIR . '/stuff/class_validator.php');
include(EASYWIDIR . '/stuff/settings.php');
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
printText('File include and parameters fetched. Start connecting to external systems.');
2013-10-16 19:56:54 +02:00
$query = $sql->prepare("SELECT * FROM `api_import` WHERE `active`='Y'");
$query2 = $sql->prepare("UPDATE `userdata` SET `salutation`=?,`mail`=?,`cname`=?,`name`=?,`vname`=?,`birthday`=?,`country`=?,`phone`=?,`fax`=?,`handy`=?,`city`=?,`cityn`=?,`street`=?,`streetn`=? WHERE `sourceSystemID`=? AND `externalID`=? AND `resellerid`=? LIMIT 1");
2013-10-16 20:59:18 +02:00
$query3 = $sql->prepare("INSERT INTO `userdata` (`accounttype`,`salutation`,`mail`,`cname`,`vname`,`name`,`birthday`,`country`,`phone`,`fax`,`handy`,`city`,`cityn`,`street`,`streetn`,`sourceSystemID`,`externalID`,`security`,`resellerid`) VALUES ('u',?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$query4 = $sql->prepare("SELECT COUNT(`id`) AS `amount` FROM `userdata` WHERE `sourceSystemID`=? AND `externalID`=? LIMIT 1");
$query5 = $sql->prepare("SELECT COUNT(`id`) AS `amount` FROM `userdata` WHERE LOWER(`mail`)=? AND LOWER(`cname`)=? LIMIT 1");
2013-10-16 20:59:18 +02:00
$query6 = $sql->prepare("UPDATE `userdata` SET `salutation`=?,`mail`=?,`cname`=?,`vname`=?,`name`=?,`birthday`=?,`country`=?,`phone`=?,`fax`=?,`handy`=?,`city`=?,`cityn`=?,`street`=?,`streetn`=? WHERE LOWER(`mail`)=? AND LOWER(`cname`)=? AND `resellerid`=? LIMIT 1");
$query7 = $sql->prepare("UPDATE `api_import` SET `lastCheck`=?,`lastID`=? WHERE `importID`=? LIMIT 1");
2013-10-16 20:59:18 +02:00
$query8 = $sql->prepare("INSERT INTO `userdata_groups` (`userID`,`groupID`,`resellerID`) VALUES (?,?,?) ON DUPLICATE KEY UPDATE `userID`=`userID`");
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
$query->execute();
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) {
2013-10-16 19:56:54 +02:00
$resellerID = $row['resellerID'];
2013-10-16 19:56:54 +02:00
$start = 0;
2013-10-13 13:11:38 +02:00
if ($row['ssl'] == 'Y') {
2013-10-16 19:56:54 +02:00
$ssl = 'https://';
$port = 443;
2013-08-28 22:47:04 +02:00
} else {
2013-10-16 19:56:54 +02:00
$ssl = 'http://';
$port = 80;
2013-08-28 22:47:04 +02:00
}
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
printText('Connect to: '.$ssl.$row['domain']);
2013-10-16 19:56:54 +02:00
while (!isset($left) or $left > 0) {
$getRequest = '/' . $row['file'] . '?passwordToken=' . urlencode($row['token']) . '&start=' . urlencode($start) . '&chunkSize=' . urlencode($row['chunkSize']) . '&lastID=' . urlencode($row['lastID']) . '&updateTime=' . urlencode($row['lastCheck']);
$rawResponse = webhostRequest($row['domain'], 'https://easy-wi.com user importer', $getRequest, null, $port);
$response = cleanFsockOpenRequest($rawResponse, '{', '}');
$decoded = json_decode($response);
2013-08-28 22:47:04 +02:00
unset($response);
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
if ($decoded and isset($decoded->error)) {
$left = 0;
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
if (is_array($decoded->error)) {
2013-10-16 19:56:54 +02:00
printText('Error: ' . implode(', ', $decoded->error));
2013-08-28 22:47:04 +02:00
} else {
2013-10-16 19:56:54 +02:00
printText('Error: ' . $decoded->error);
2013-08-28 22:47:04 +02:00
}
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
} else if ($decoded and isset($decoded->total)) {
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
if (isset($left)) {
2013-10-16 19:56:54 +02:00
$left -= $row['chunkSize'];
2013-08-28 22:47:04 +02:00
} else {
2013-10-16 19:56:54 +02:00
$left = $decoded->total - $row['chunkSize'];
2013-08-28 22:47:04 +02:00
}
2013-10-16 19:56:54 +02:00
$start += $row['chunkSize'];
2013-08-28 22:47:04 +02:00
unset($lastID);
2013-10-16 19:56:54 +02:00
2013-10-16 20:59:18 +02:00
foreach ($decoded->entries as $value) {
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
if (isset($value->externalID)) {
2013-10-16 19:56:54 +02:00
$query4->execute(array(json_encode(array('I' => $row['importID'])), $value->externalID));
$checkAmount = $query4->fetchColumn();
2013-10-16 19:56:54 +02:00
if ($checkAmount > 0 and $row['fetchUpdates'] == 'Y') {
$query2->execute(array(getParam('salutation'), strtolower(getParam('email')), getParam('loginName'), getParam('firstName'), getParam('lastName'), getParam('birthday'), getParam('country'), getParam('phone'), getParam('fax'), getParam('handy'), getParam('city'), getParam('cityn'), getParam('street'), getParam('streetn'),json_encode(array('I' => $row['importID'])), getParam('externalID'), $row['resellerID']));
printText('User updated. Loginname: ' . $value->loginName.' e-mail: ' . strtolower($value->email));
} else if ($checkAmount > 0) {
printText('User update skipped. Loginname: ' . $value->loginName.' e-mail: ' . strtolower($value->email));
2013-08-28 22:47:04 +02:00
} else {
2013-10-16 19:56:54 +02:00
$query5->execute(array(strtolower($value->email), strtolower($value->loginName)));
if ($query5->fetchColumn()>0 and $row['fetchUpdates'] == 'Y') {
2013-10-16 20:59:18 +02:00
$query6->execute(array(getParam('salutation'), strtolower(getParam('email')), getParam('loginName'), getParam('firstName'), getParam('lastName'), getParam('birthday'), getParam('country'), getParam('phone'), getParam('fax'), getParam('handy'), getParam('city'), getParam('cityn'), getParam('street'), getParam('streetn'), strtolower($value->email), strtolower($value->loginName), $row['resellerID']));
2013-10-16 19:56:54 +02:00
printText('User updated. Loginname: ' . $value->loginName.' e-mail: ' . strtolower($value->email));
} else if ($checkAmount > 0) {
printText('User update skipped because source system differ. Loginname: ' . $value->loginName.' e-mail: ' . strtolower($value->email));
2013-08-28 22:47:04 +02:00
} else {
2013-10-16 19:56:54 +02:00
printText('Import user. Loginname: ' . $value->loginName.' e-mail: ' . strtolower($value->email));
2013-10-16 20:59:18 +02:00
$query3->execute(array(getParam('salutation'), strtolower(getParam('email')), getParam('loginName'), getParam('firstName'), getParam('lastName'), getParam('birthday'), getParam('country'), getParam('phone'), getParam('fax'), getParam('handy'), getParam('city'), getParam('cityn'), getParam('street'), getParam('streetn'), json_encode(array('I' => $row['importID'])), getParam('externalID'), getParam('password'), $row['resellerID']));
$query8->execute(array($sql->lastInsertId(), $row['groupID'], $row['resellerID']));
2013-08-28 22:47:04 +02:00
}
}
2013-10-16 19:56:54 +02:00
if (getParam('updatetime') != '' and (isset($lastCheck) and strtotime(getParam('updatetime')) > strtotime($lastCheck)) or !isset($lastCheck)) {
$lastCheck = getParam('updatetime');
2013-08-28 22:47:04 +02:00
}
2013-10-16 19:56:54 +02:00
$lastID = $value->externalID;
2013-08-28 22:47:04 +02:00
}
}
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
if (isset($lastID)) {
if (!isset($lastCheck)) {
$lastCheck=date('Y-m-d H:i:s');
}
2013-10-16 19:56:54 +02:00
$query7->execute(array($lastCheck, $lastID, $row['importID']));
2013-08-28 22:47:04 +02:00
}
2013-10-16 19:56:54 +02:00
if ($left > 0){
printText('Total amount is: ' . $decoded->total . ' User left: ' . $left . ' need to make another run');
2013-08-28 22:47:04 +02:00
sleep(1);
} else {
2013-10-16 19:56:54 +02:00
printText('Total amount is: ' . $decoded->total . ' No user left.');
2013-08-28 22:47:04 +02:00
}
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
} else if ($decoded) {
printText('JSON Response does not contain expected values');
$left = 0;
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
} else {
2013-10-16 19:56:54 +02:00
if (strpos(strtolower($rawResponse), 'file not found') === false) {
2013-08-28 22:47:04 +02:00
printText('No Json Response. Will retry.');
} else {
$left = 0;
2013-08-28 22:47:04 +02:00
printText('404: File not found');
}
}
}
}
2013-10-16 19:56:54 +02:00
$query = $sql->prepare("UPDATE `settings` SET `lastCronCloud`=UNIX_TIMESTAMP() WHERE `resellerid`=0 LIMIT 1");
2013-08-28 22:47:04 +02:00
$query->execute();
2013-10-16 19:56:54 +02:00
2013-08-28 22:47:04 +02:00
} else {
header('Location: login.php');
die('Cloud can only be run via console and or a cronjob');
}