/private/syncBusinessInfo.php

https://github.com/ciniki/core · PHP · 139 lines · 83 code · 9 blank · 47 comment · 21 complexity · 59cb5854f620bd70355e0f9c4d0f1ecf MD5 · raw file

  1. <?php
  2. //
  3. // Description
  4. // -----------
  5. // This function will return the info about a tenant. This information is used
  6. // to compare with the remote system to determine if the two are compatitable for
  7. // a sync.
  8. //
  9. // Arguments
  10. // ---------
  11. // ciniki:
  12. // tnid: The ID of the tenant to get the sync information for.
  13. //
  14. // Returns
  15. // -------
  16. // <modules>
  17. // <module name="ciniki.artcatalog" permissions="" version="20121226.2232">
  18. // <tables>
  19. // <table name="ciniki_artcatalog" version="v1.01" />
  20. // <table name="ciniki_artcatalog_history" version="v1.01" />
  21. // </tables>
  22. // </module>
  23. // </modules>
  24. //
  25. function ciniki_core_syncTenantInfo($ciniki, $tnid) {
  26. //
  27. // Check to make sure a tenant is specified
  28. //
  29. if( $tnid < 1 ) {
  30. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.180', 'msg'=>'No tenant specified'));
  31. }
  32. //
  33. // Grab the _versions.ini info
  34. //
  35. if( !file_exists($ciniki['config']['ciniki.core']['root_dir'] . "/_versions.ini") ) {
  36. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.181', 'msg'=>'Unable to get module version information'));
  37. }
  38. $modules_ini = parse_ini_file($ciniki['config']['ciniki.core']['root_dir'] . "/_versions.ini", true);
  39. //
  40. // Result array
  41. //
  42. $rsp = array('stat'=>'ok', 'tables'=>array(), 'tenant'=>array('modules'=>array()));
  43. //
  44. // Get all the table versions
  45. //
  46. $strsql = "SHOW TABLE STATUS";
  47. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'dbHashIDQuery');
  48. $rc = ciniki_core_dbHashIDQuery($ciniki, $strsql, 'ciniki.core', 'tables', 'Name');
  49. if( $rc['stat'] != 'ok' ) {
  50. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.182', 'msg'=>'Unable to get table versions', 'err'=>$rc['err']));
  51. }
  52. if( !isset($rc['tables']) ) {
  53. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.183', 'msg'=>'Unable to get table versions'));
  54. }
  55. $db_tables = $rc['tables'];
  56. //
  57. // Get modules which are enabled for the tenant, and their checksums
  58. //
  59. $strsql = "SELECT CONCAT_WS('.', package, module) AS fname, "
  60. . "package, module AS name, UNIX_TIMESTAMP(last_change) AS last_change "
  61. . "FROM ciniki_tenant_modules "
  62. . "WHERE tnid = '" . ciniki_core_dbQuote($ciniki, $tnid) . "' "
  63. . "AND (status = 1 OR status = 2) "
  64. . "";
  65. ciniki_core_loadMethod($ciniki, 'ciniki', 'core', 'private', 'dbHashQueryTree');
  66. $rc = ciniki_core_dbHashQueryTree($ciniki, $strsql, 'ciniki.tenants', array(
  67. array('container'=>'modules', 'fname'=>'fname', 'name'=>'module',
  68. 'fields'=>array('package', 'name', 'last_change')),
  69. ));
  70. if( $rc['stat'] != 'ok' ) {
  71. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.184', 'msg'=>'Unable to get active modules', 'err'=>$rc['err']));
  72. }
  73. if( !isset($rc['modules']) ) {
  74. return array('stat'=>'fail', 'err'=>array('code'=>'ciniki.core.185', 'msg'=>'Unable to get active modules'));
  75. }
  76. $modules = $rc['modules'];
  77. $mods = array();
  78. foreach($modules as $mid => $module) {
  79. $mod[$module['module']['package'] . '.' . $module['module']['name']] = $module['module']['last_change'];
  80. }
  81. if( !isset($mods['ciniki.users']) ) {
  82. $modules[] = array('module'=>array('package'=>'ciniki', 'name'=>'users', 'last_change'=>0));
  83. }
  84. if( !isset($mods['ciniki.images']) ) {
  85. $modules[] = array('module'=>array('package'=>'ciniki', 'name'=>'images', 'last_change'=>0));
  86. }
  87. if( !isset($mods['ciniki.tenants']) ) {
  88. $modules[] = array('module'=>array('package'=>'ciniki', 'name'=>'tenants', 'last_change'=>0));
  89. }
  90. //
  91. // Check each package/module for table.schema's and get version from database
  92. //
  93. foreach($modules as $mnum => $module) {
  94. //
  95. // Setup module version
  96. //
  97. if( isset($modules_ini[$module['module']['package'] . '.api.' . $module['module']['name']]) ) {
  98. $modules[$mnum]['module']['version'] = $modules_ini[$module['module']['package'] .'.api.' . $module['module']['name']]['version'];
  99. $modules[$mnum]['module']['hash'] = $modules_ini[$module['module']['package'] .'.api.' . $module['module']['name']]['hash'];
  100. } else {
  101. $modules[$mnum]['module']['version'] = '';
  102. $modules[$mnum]['module']['hash'] = '';
  103. }
  104. $modules[$mnum]['module']['tables'] = array();
  105. $dir = $ciniki['config']['core']['root_dir'] . '/' . $module['module']['package'] . '-mods/' . $module['module']['name'] . '/db';
  106. if( !is_dir($dir) ) {
  107. continue;
  108. }
  109. $dh = opendir($dir);
  110. if( $dh == false ) {
  111. continue;
  112. }
  113. while( false !== ($filename = readdir($dh))) {
  114. if( $filename[0] == '.' ) {
  115. continue;
  116. }
  117. if( preg_match('/^(.*)\.schema$/', $filename, $matches) ) {
  118. $table = $matches[1];
  119. if( isset($db_tables[$table]) && preg_match('/(v[0-9]+\.[0-9]+)([^0-9]|$)/i', $db_tables[$table]['Comment'], $matches) ) {
  120. array_push($modules[$mnum]['module']['tables'], array('table'=>array('name'=>$table, 'version'=>$matches[1])));
  121. }
  122. }
  123. }
  124. }
  125. //
  126. // Return information
  127. //
  128. return array('stat'=>'ok', 'modules'=>$modules);
  129. }
  130. ?>