Explorar o código

Add app bootstrap and DBG, ACL

Piotr Labudda %!s(int64=10) %!d(string=hai) anos
pai
achega
57cbe37168
Modificáronse 4 ficheiros con 311 adicións e 23 borrados
  1. 4 23
      SE/index.php
  2. 170 0
      SE/se-lib/ACL.php
  3. 115 0
      SE/se-lib/DBG.php
  4. 22 0
      SE/se-lib/bootstrap.php

+ 4 - 23
SE/index.php

@@ -1,11 +1,6 @@
 <?php
 
-define('DS', DIRECTORY_SEPARATOR);
-define('APP_PATH_ROOT', dirname(__FILE__));
-define('APP_PATH_LIB', APP_PATH_ROOT . '/se-lib');
-define('APP_PATH_WWW', APP_PATH_ROOT);
-define('APP_PATH_CONFIG', APP_PATH_ROOT . DS . 'config');
-define('APP_PATH_SCHEMA', APP_PATH_ROOT . DS . 'schema');
+require_once dirname(__FILE__) . '/se-lib/bootstrap.php';
 
 //session_save_path("./tmp") ;
 session_start();
@@ -19,28 +14,14 @@ ini_set('error_reporting', $errorReportingLevel);
 #TEST $_SESSION['DEBUG'] = 3;// TODO: TEST
 if (!isset($_SESSION['DEBUG'])) $_SESSION['DEBUG'] = 0;// set default value
 
-
-if (file_exists(APP_PATH_ROOT . "/config/.config_{$_SERVER['SERVER_NAME']}.php")) {
-	require APP_PATH_ROOT . "/config/.config_{$_SERVER['SERVER_NAME']}.php";
+if (file_exists(APP_PATH_CONFIG . DS . ".config_{$_SERVER['SERVER_NAME']}.php")) {
+	require APP_PATH_CONFIG . DS . ".config_{$_SERVER['SERVER_NAME']}.php";
 }
-
-if (file_exists(APP_PATH_ROOT . "/.config.php")) include APP_PATH_ROOT . "/.config.php";
-
+if (file_exists(APP_PATH_ROOT . DS . ".config.php")) include APP_PATH_ROOT . DS . ".config.php";
 
 require_once APP_PATH_ROOT . "/superedit-SEF.php";
 SEF('DEBUG_S');
 
-require_once APP_PATH_LIB . '/' . 'Lib.php';
-Lib::loadClass('V');
-Lib::loadClass('DB');
-Lib::loadClass('User');
-Lib::loadClass('SE_Layout');
-Lib::loadClass('S');
-Lib::loadClass('Http');
-Lib::loadClass('HttpException');
-Lib::loadClass('StorageException');
-Lib::loadClass('Router');
-
 S::init();// init session variables if not exists
 User::auth();// die if not logged in
 

+ 170 - 0
SE/se-lib/ACL.php

@@ -0,0 +1,170 @@
+<?php
+
+
+class ACL {
+
+	/**
+	 * Ids List of Proces Init for given tabel (skip filters)
+	 */
+	public function getTableProcesInitIds($idTable) {// TODO: use in TableAjax
+		$procesInitList = self::getTableProcesInitList($idTable);
+		return array_keys($procesInitList);
+	}
+
+	/**
+	 * List of Proces Init for given table (skip filters)
+	 */
+	public function getTableProcesInitList($idTable) {// TODO: use in TableAjax
+		$tableProcesInitList = array();
+		$sqlIdProcesListSql = <<<SQL
+			select tpv.`ID_PROCES`
+				from `CRM_PROCES_idx_TABLE_TO_PROCES_VIEW` tpv
+				where tpv.`ID_TABLE`='{$idTable}'
+SQL;
+		$fetchTableProcesInitListSql = <<<SQL
+			-- time ~0.07 -- no goto and return
+			select p.`ID`, p.`DESC`
+			from `CRM_PROCES` p
+			where p.`ID` in(
+						select i.`idx_PROCES_INIT_ID`
+						from `CRM_PROCES_idx` i
+						where i.`ID_PROCES` in({$sqlIdProcesListSql})
+				)
+				and p.`TYPE`='PROCES_INIT'
+			order by p.`SORT_PRIO`
+SQL;
+		/*
+			SELECT p.`ID` , p.`DESC`
+			FROM  `CRM_PROCES` p
+			WHERE p.`ID`
+			IN (
+					SELECT i.`idx_PROCES_INIT_ID`
+					FROM  `CRM_PROCES_idx` i
+					WHERE i.`ID_PROCES`
+					IN (
+							SELECT tpv.`ID_PROCES`
+							FROM  `CRM_PROCES_idx_TABLE_TO_PROCES_VIEW` tpv
+							WHERE tpv.`ID_TABLE` =  '13051'
+					)
+			)
+			AND p.`TYPE` =  'PROCES_INIT'
+			order by p.`SORT_PRIO`
+		*/
+		$fetchTableProcesInitListSql = <<<SQL
+			-- time ~0.15s
+			select p.`ID`, p.`DESC`
+			from `CRM_PROCES` p
+			where p.`ID` in(
+						select i.`idx_PROCES_INIT_ID`
+						from `CRM_PROCES_idx` i
+						where i.`ID_PROCES` in({$sqlIdProcesListSql})
+					union
+						select ig.`idx_PROCES_INIT_ID`
+						from `CRM_PROCES_idx` i
+							join `CRM_PROCES_idx` ig on(ig.`ID_PROCES`=i.`idx_PROCES_WITH_GROUPS_ID`)
+						where i.`ID_PROCES` in({$sqlIdProcesListSql})
+				)
+				and p.`TYPE`='PROCES_INIT'
+			order by p.`SORT_PRIO`
+SQL;
+		$fetchTableProcesInitListSql = <<<SQL
+			-- time ~0.14
+			select p.`ID`, p.`DESC`
+			from `CRM_PROCES` p
+			where p.`ID` in(
+					select i.`idx_PROCES_INIT_ID`
+					from `CRM_PROCES_idx` i
+					where i.`ID_PROCES` in({$sqlIdProcesListSql})
+						or i.`ID_PROCES` in(
+							select ig.`idx_PROCES_WITH_GROUPS_ID`
+							from `CRM_PROCES_idx` ig
+							where ig.`ID_PROCES` in({$sqlIdProcesListSql})
+						)
+				)
+				and p.`TYPE`='PROCES_INIT'
+			order by p.`SORT_PRIO`
+SQL;
+		//echo'<pre>$fetchTableProcesInitListSql('.$idTable.') ';print_r($fetchTableProcesInitListSql);echo'</pre>';
+		$tableProcesInitList = array();
+		$db = DB::getDB();
+		$res = $db->query($fetchTableProcesInitListSql);
+		while ($r = $db->fetch($res)) {
+			$tableProcesInitList[$r->ID] = $r->DESC;
+		}
+		return $tableProcesInitList;
+	}
+
+	public static function getProcesInitMapTreeOnlyIds($ids) {
+		$mapTree = array();
+		$map = self::getProcesInitMapOnlyIds($ids);
+		foreach ($map as $r) {
+			if ('PROCES_INIT' == $r->TYPE) {
+				$mapTree[$r->ID_PROCES] = array();
+			}
+		}
+		foreach ($map as $r) {
+			if ('GOTO_AND_RETURN' == $r->TYPE) {
+				$mapTree[$r->idx_MAIN_PROCES_INIT_ID][$r->ID_PROCES] = array();
+			}
+		}
+		foreach ($map as $r) {
+			if ('GOTO_AND_RETURN_LVL2' == $r->TYPE) {
+				$mapTree[$r->idx_MAIN_PROCES_INIT_ID][$r->idx_GOTO_LVL2_INIT_ID][$r->ID_PROCES] = true;
+			}
+		}
+		return $mapTree;
+	}
+
+	public static function getProcesInitMapOnlyIds($ids) {
+		$map = array();
+		$sqlIds = V::filter($ids, array('V', 'filterPositiveInteger'));
+		$sqlIds = implode(',', $sqlIds);
+		if (empty($sqlIds)) return $map;
+		$sql = <<<SQL
+			select i.`ID_PROCES`
+				, i.`PARENT_ID`
+				, i.`TYPE`
+				, i.`idx_PROCES_INIT_ID`
+				, i.`idx_MAIN_PROCES_INIT_ID`
+				, i.`idx_PROCES_WITH_GROUPS_ID`
+				, IF(i.`TYPE`='GOTO_AND_RETURN_LVL2'
+					, (select ig.`idx_PROCES_INIT_ID`
+						from `CRM_PROCES_idx` ig
+						where ig.`ID_PROCES`=i.`PARENT_ID`
+						limit 1)
+					, 0
+				) as idx_GOTO_LVL2_INIT_ID
+			from `CRM_PROCES_idx` i
+			where i.`ID_PROCES` in({$sqlIds})
+				and i.`idx_MAIN_PROCES_INIT_ID` in({$sqlIds})
+SQL;
+		DBG::_('DBG_MAP', '1', "MAP SQL", $sql, __CLASS__, __FUNCTION__, __LINE__);
+		$db = DB::getDB();
+		$res = $db->query($sql);
+		while ($r = $db->fetch($res)) {
+			$map[] = $r;
+		}
+		//DBG::table("MAP", $map, __CLASS__, __FUNCTION__, __LINE__);
+		return $map;
+	}
+
+	public function canGroupViewProces($idGroup, $idProcesInit) {
+		$isAllowed = false;
+		$idProcesInit = (int)$idProcesInit;
+		if (!$idProcesInit) return false;
+		$checkProcesAccessSql = <<<SQL
+			select count(*) as cnt
+				from `CRM_PROCES_idx_GROUP_to_INIT_VIEW` giv
+				where giv.`ID_GROUP` = '{$idGroup}'
+					and giv.`ID_PROCES_INIT` = '{$idProcesInit}'
+SQL;
+		$db = DB::getDB();
+		$res = $db->query($checkProcesAccessSql);
+		if ($r = $db->fetch($res)) {
+			if ($r->cnt > 0) {
+				$isAllowed = true;
+			}
+		}
+		return $isAllowed;
+	}
+}

+ 115 - 0
SE/se-lib/DBG.php

@@ -0,0 +1,115 @@
+<?php
+
+class DBG {
+
+	/**
+	 * @param $reqKqy - key in $_REQUEST array (if true then always show)
+	 * @param $reqValueExpr - expression to compare req value
+	 *   true - always visible - only for fast DBG without $reqKey in REQUEST
+	 *   '>*', '>=*', '<*', '<=*' - compare $reqValue
+	 * examples:
+	 *   - show when $_REQUEST['DBG_SCH'] == '1'
+	 *     DBG::_('DBG_SCH', '1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
+	 *   - show when $_REQUEST['DBG_SCH'] > '1'
+	 *     DBG::_('DBG_SCH', '>1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
+	 *   - show when any value: strlen($_REQUEST['DBG_SCH']) > 0
+	 *     DBG::_('DBG_SCH', true, "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
+	 *   - always show
+	 *     DBG::_(true, true, "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
+	 */
+	public static function _($reqKey, $reqValueExpr, $label, $variable, $className, $functionName, $lineNumber, $borderColor = 'red') {
+		$showDBG = false;
+		$reqValue = V::get($reqKey, '', $_GET);
+		if (true === $reqKey) {
+			$showDBG = true;
+		}
+		else if (strlen($reqValue) == 0) {
+			return;
+		}
+		else if ($reqValue == $reqValueExpr) {
+			$showDBG = true;
+		}
+		else if (true === $reqValueExpr) {
+			$showDBG = true;
+		}
+		else {
+			if ('>=' == substr($reqValueExpr, 0, 2)) {
+				if ($reqValue >= substr($reqValueExpr, 2)) {
+					$showDBG = true;
+				}
+			}
+			else if ('>' == substr($reqValueExpr, 0, 1)) {
+				if ($reqValue > substr($reqValueExpr, 1)) {
+					$showDBG = true;
+				}
+			}
+			else if ('<=' == substr($reqValueExpr, 0, 2)) {
+				if ($reqValue <= substr($reqValueExpr, 2)) {
+					$showDBG = true;
+				}
+			}
+			else if ('<' == substr($reqValueExpr, 0, 1)) {
+				if ($reqValue < substr($reqValueExpr, 1)) {
+					$showDBG = true;
+				}
+			}
+			else {
+				if ($reqValue = $reqValueExpr) {
+					$showDBG = true;
+				}
+			}
+		}
+		if ($showDBG) {
+			?>
+<pre style="max-height:200px;max-width:800px;overflow:auto;border:1px solid <?php echo $borderColor; ?>;text-align:left;"
+		 ><?php echo "{$label} ({$className}::{$functionName}:{$lineNumber}):\n"; ?>
+<?php print_r($variable);?>
+</pre>
+<?php
+		}
+	}
+
+	public static function table($label, $table, $className, $functionName, $lineNumber) {
+		$cols = array();
+		if (is_array($table) && !empty($table)) {
+			$firstRow = array();
+			foreach ($table as $row) {
+				$firstRow = $row;
+				break;
+			}
+			if (is_array($firstRow)) {
+				$cols = array_keys($firstRow);
+			}
+			else if (is_object($firstRow)) {
+				$cols = array_keys((array)$firstRow);
+			}
+			else {
+				return;// bad table item type
+			}
+		}
+		?>
+<table class="table table-bordered">
+	<caption><?php echo "{$label} ({$className}::{$functionName}:{$lineNumber}):"; ?></caption>
+	<thead>
+		<tr>
+			<th style="padding:2px;">Lp.</th>
+		<?php foreach ($cols as $colName) : ?>
+			<th style="padding:2px;"><?php echo $colName; ?></th>
+		<?php endforeach; ?>
+		</tr>
+	</thead>
+	<tbody>
+	<?php $i = 0; foreach ($table as $row) : $i++; ?>
+		<tr>
+			<td style="padding:2px;"><?php echo $i; ?></td>
+		<?php foreach ($cols as $colName) : ?>
+			<td style="padding:2px;"><?php echo V::get($colName, '', $row); ?></td>
+		<?php endforeach; ?>
+		</tr>
+	<?php endforeach; ?>
+	</tbody>
+</table>
+<?php
+	}
+
+}

+ 22 - 0
SE/se-lib/bootstrap.php

@@ -0,0 +1,22 @@
+<?php
+
+define('DS', DIRECTORY_SEPARATOR);
+define('APP_PATH_LIB', dirname(__FILE__));
+define('APP_PATH_ROOT', APP_PATH_LIB . DS . '..');
+define('APP_PATH_WWW', APP_PATH_ROOT);
+define('APP_PATH_CONFIG', APP_PATH_ROOT . DS . 'config');
+define('APP_PATH_SCHEMA', APP_PATH_ROOT . DS . 'schema');
+
+require_once APP_PATH_LIB . '/' . 'Lib.php';
+Lib::loadClass('DBG');
+Lib::loadClass('V');
+Lib::loadClass('Config');
+Lib::loadClass('DB');
+Lib::loadClass('ACL');
+Lib::loadClass('User');
+Lib::loadClass('SE_Layout');
+Lib::loadClass('S');
+Lib::loadClass('Http');
+Lib::loadClass('HttpException');
+Lib::loadClass('StorageException');
+Lib::loadClass('Router');