Jelajahi Sumber

updated Cron: fixed sql, added send task

Piotr Labudda 10 tahun lalu
induk
melakukan
b51a10fb45
2 mengubah file dengan 112 tambahan dan 8 penghapusan
  1. 40 0
      SE/se-lib/Route/Cron-test.php
  2. 72 8
      SE/se-lib/Route/Cron.php

+ 40 - 0
SE/se-lib/Route/Cron-test.php

@@ -0,0 +1,40 @@
+<?php
+
+require_once dirname(__FILE__) . '/../bootstrap.php';
+
+if (isset($_SERVER["argv"][1])) {
+	$_SERVER['SERVER_NAME'] = $_SERVER["argv"][1];
+} else {
+  die("\n The second argument should be server domain - e.g. biuro.biall-net.pl \n");
+}
+
+Lib::loadClass('Router');
+$keyToken = 'bach_sync_perms';
+$token = Router::getRoute('Cron')->generateCliAuthToken($keyToken, 'test', 300 * 10);
+echo "DBG: token = '{$token}'" . "\n";
+
+//echo "DBG: IP(biuro.biall-net.pl) = '" . gethostbyname('biuro.biall-net.pl') . "'" . "\n";
+//echo "DBG: IP(https://biuro.biall-net.pl) = '" . gethostbyname('https://biuro.biall-net.pl') . "'" . "\n";
+
+$baseUrl = "https://{$_SERVER['SERVER_NAME']}/SE";
+if ('biuro.biall-net.pl' == $_SERVER['SERVER_NAME']) $baseUrl = "https://{$_SERVER['SERVER_NAME']}/SE/version-git";
+if ('biuro.biall-net.pl' == $_SERVER['SERVER_NAME']) $baseUrl = "https://{$_SERVER['SERVER_NAME']}/dev-pl/se-master";
+//file_get_contents("https://{$baseUrl}/index.php?_route=Cron&_key={$keyToken}&_token={$token}&_task=test");
+
+function runUrlTask($url) {
+  $ch = curl_init();
+  curl_setopt($ch, CURLOPT_VERBOSE, true);
+  curl_setopt($ch, CURLOPT_URL, $url);
+  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
+  $return = curl_exec($ch);
+  curl_close($ch);
+  return $return;
+}
+//$string = runUrlTask("https://{$baseUrl}/index.php?_route=Cron&_key={$keyToken}&_token={$token}&_task=test");
+$string = runUrlTask("{$baseUrl}/index.php?_route=Cron&_key={$keyToken}&_token={$token}&_task=test");
+echo "DBG: string -----------------------\n{$string}\nEOF string---------------------------------\n";
+
+die("\n\n.EOF\n");
+?>

+ 72 - 8
SE/se-lib/Route/Cron.php

@@ -6,23 +6,26 @@ Lib::loadClass('RouteBase');
  * usage example - cli script:
  * $token = Router::getRoute('Cron')->generateCliAuthToken('bach_sync_perms', 300);
  * file_get_contents("https://{$baseUrl}/index.php?_route=Cron&_key=bach_sync_perms&_token={$token}&_task=run");
+ *
+ * TEST: $ php SE/se-lib/Route/Cron-test.php biuro.biall-net.pl
  */
 class Route_Cron extends RouteBase {
 
 	public function handleAuth() {
 		if (User::logged()) {
 
-    } else if ($this->authByCliToken()) {
+    } else if ($this->authByToken()) {
 
     } else {
 			throw new HttpException('Unauthorized', 401);
 		}
 	}
 
-	public function generateCliAuthToken($cliKey, $ttl = 300) {
+	public function generateCliAuthToken($cliKey, $task, $ttl = 300) {
 		$generatedToken = uniqid();
     $parts = array();
     $parts[] = $generatedToken;
+		$parts[] = $task;
     $parts[] = $ttl;
     $parts[] = time();
     $token = implode(",", $parts);
@@ -30,7 +33,7 @@ class Route_Cron extends RouteBase {
     $sth = DB::getPDO()->prepare("
       insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
         values ( :cliKey, :token )
-        on duplicate key update set CONF_VAL = :token
+        on duplicate key update CONF_VAL = :token
     ");
 		$sth->bindValue(':cliKey', $sqlCliKey, PDO::PARAM_STR);
 		$sth->bindValue(':token', $token, PDO::PARAM_STR);
@@ -38,17 +41,46 @@ class Route_Cron extends RouteBase {
     return $generatedToken;
   }
 
-	public function authByCliAuthToken() {
+	public function authByToken() {
 		$cliKey = V::get('_key', '', $_REQUEST);
     $cliToken = V::get('_token', '', $_REQUEST);
 
 		$sqlCliKey = "CronCliAuthToken:{$cliKey}";
-		// select from CRM_CONFIG where CONF_KEY = $sqlCliKey
-		// unpack token
-		// check ttl
+		$sth = DB::getPDO()->prepare("
+      select c.CONF_VAL
+			from CRM_CONFIG c
+      where CONF_KEY = :cliKey
+			order by c.ID desc
+			limit 1
+    ");
+		$sth->bindValue(':cliKey', $sqlCliKey, PDO::PARAM_STR);
+		$sth->execute();
+		$rawToken = $sth->fetch();
+		if (!$rawToken || !$rawToken['CONF_VAL']) throw new HttpException("Unauthorized - token not found #1-" . __LINE__, 401);
+		$rawToken = explode(',', $rawToken['CONF_VAL']);
+		DBG::_('DBG_CRON', '>1', 'rawToken', $rawToken, __CLASS__, __FUNCTION__, __LINE__);
+		if (4 != count($rawToken)) throw new HttpException("Unauthorized - token not found #2-" . __LINE__, 401);
+		if ($cliToken != $rawToken[0]) throw new HttpException("Unauthorized - token not found #3-" . __LINE__, 401);
+
+		$task = $rawToken[1];
+		$ttl = (int)$rawToken[2];
+		$createDateTimestamp = (int)$rawToken[3];
+
+		if (!$ttl) throw new HttpException("Unauthorized - token not found #4-" . __LINE__, 401);
+		if (!$createDateTimestamp) throw new HttpException("Unauthorized - token not found #5-" . __LINE__, 401);
+		DBG::_('DBG_CRON', '>1', 'rawToken', array('createDateTimestamp'=>$createDateTimestamp, 'ttl'=>$ttl, 'cur'=>time()), __CLASS__, __FUNCTION__, __LINE__);
+		if ($createDateTimestamp + $ttl < time()) {
+			// TODO: remove record from table?
+			throw new HttpException("Unauthorized - token expired #6-" . __LINE__, 401);
+		}
 
 		session_write_close();// changes in $_SESSION visible only in current process
-		//$_SESSION[''] = '';
+		$_SESSION['AUTHORIZE_USER'] = 'anonymous';
+		$_SESSION['ADM_NAME'] = 'Anonymous';
+		$_SESSION['ADM_ACCOUNT'] = $_SERVER['REMOTE_ADDR'];
+		$_SESSION['ADM_ADMIN_LEVEL'] = 10;
+		DBG::_('DBG_CRON', '>1', 'rawToken', array('createDateTimestamp'=>$createDateTimestamp, 'ttl'=>$ttl, 'cur'=>time()), __CLASS__, __FUNCTION__, __LINE__);
+		$this->runTask($task);
   }
 
 	public function defaultAction() {
@@ -62,4 +94,36 @@ class Route_Cron extends RouteBase {
 		SE_Layout::dol();
 	}
 
+	public function testAction() {
+		$notify = Router::getRoute('Notify');
+		$todoReminders = array();
+
+		{// limit send time to 8 - 20
+			$timeNow = time();
+			$timeSendLimitFrom = mktime(8, 0, 0, date('n'), date('j'), date('Y'));
+			$timeSendLimitTo = mktime(20, 0, 0, date('n'), date('j'), date('Y'));
+			if ($timeNow > $timeSendLimitFrom && $timeNow < $timeSendLimitTo) {
+				$todoReminders = $notify->getTodoList(100);
+			}
+		}
+
+		?>
+<div class="container">
+	<h1>Cron</h1>
+	<?php DBG::_(true, true, 'todoReminders', $todoReminders, __CLASS__, __FUNCTION__, __LINE__); ?>
+</div>
+		<?php
+
+		foreach ($todoReminders as $who => $listWhen) {
+			foreach ($listWhen as $when => $listWhat) {
+				if (!empty($listWhat)) {
+					$notify->send($who, array_keys($listWhat));// , $forceMail = 'plabudda@biall-net.pl'
+					$notify->markAsSent($usrLogin, array_keys($listWhat));
+				}
+			}
+		}
+
+		echo "\n.EOF\n";
+	}
+
 }