/backend/php/src/objects/class.pog_base.php

https://github.com/gcsolaroli/password-manager · PHP · 143 lines · 96 code · 14 blank · 33 comment · 16 complexity · 4450c34f7cf7304a73f24d6d38d84b87 MD5 · raw file

  1. <?php
  2. class POG_Base
  3. {
  4. /**
  5. * Overloading
  6. */
  7. function __call($method, $argv)
  8. {
  9. include_once($GLOBALS['configuration']['plugins_path']."/IPlugin.php");
  10. include_once($GLOBALS['configuration']['plugins_path']."/plugin.".strtolower($method).".php");
  11. eval('$plugin = new $method($this,$argv);');
  12. return $plugin->Execute();
  13. }
  14. /**
  15. * constructor
  16. *
  17. * @return POG_Base
  18. */
  19. private function POG_Base()
  20. {
  21. }
  22. function SetFieldAttribute($fieldName, $attributeName, $attributeValue)
  23. {
  24. if (isset($this->pog_attribute_type[$fieldName]) && isset($this->pog_attribute_type[$fieldName][$attributeName]))
  25. {
  26. $this->pog_attribute_type[$fieldName][$attributeName] = $attributeValue;
  27. }
  28. }
  29. function GetFieldAttribute($fieldName, $attributeName)
  30. {
  31. if (isset($this->pog_attribute_type[$fieldName]) && isset($this->pog_attribute_type[$fieldName][$attributeName]))
  32. {
  33. return $this->pog_attribute_type[$fieldName][$attributeName];
  34. }
  35. return null;
  36. }
  37. ///////////////////////////
  38. // Data manipulation
  39. ///////////////////////////
  40. /**
  41. * This function will try to encode $text to base64, except when $text is a number. This allows us to Escape all data before they're inserted in the database, regardless of attribute type.
  42. * @param string $text
  43. * @return string encoded to base64
  44. */
  45. public function Escape($text)
  46. {
  47. if ($GLOBALS['configuration']['db_encoding'] && !is_numeric($text))
  48. {
  49. return base64_encode($text);
  50. }
  51. return addslashes($text);
  52. }
  53. /**
  54. * Enter description here...
  55. *
  56. * @param unknown_type $text
  57. * @return unknown
  58. */
  59. public function Unescape($text)
  60. {
  61. if ($GLOBALS['configuration']['db_encoding'] && !is_numeric($text))
  62. {
  63. return base64_decode($text);
  64. }
  65. return stripcslashes($text);
  66. }
  67. ////////////////////////////////
  68. // Table -> Object Mapping
  69. ////////////////////////////////
  70. /**
  71. * Executes $query against database and returns the result set as an array of POG objects
  72. *
  73. * @param string $query. SQL query to execute against database
  74. * @param string $objectClass. POG Object type to return
  75. * @param bool $lazy. If true, will also load all children/sibling
  76. */
  77. public function FetchObjects($query, $objectClass, $lazy = true)
  78. {
  79. $databaseConnection = Database::Connect();
  80. $result = Database::Query($query, $databaseConnection);
  81. $objectList = $this->CreateObjects($result, $objectClass, $lazy);
  82. return $objectList;
  83. }
  84. private function CreateObjects($mysql_result, $objectClass, $lazyLoad = true)
  85. {
  86. $objectList = array();
  87. while ($row = mysql_fetch_assoc($mysql_result))
  88. {
  89. $pog_object = new $objectClass();
  90. $this->PopulateObjectAttributes($row, $pog_object);
  91. $objectList[] = $pog_object;
  92. }
  93. return $objectList;
  94. }
  95. private function PopulateObjectAttributes($fetched_row, $pog_object)
  96. {
  97. foreach ($this->GetAttributes($pog_object) as $column)
  98. {
  99. $pog_object->{$column} = $this->Unescape($fetched_row[strtolower($column)]);
  100. }
  101. return $pog_object;
  102. }
  103. private function GetAttributes($object)
  104. {
  105. $columns = array();
  106. foreach ($object->pog_attribute_type as $att => $properties)
  107. {
  108. if ($properties['db_attributes'][0] != 'OBJECT')
  109. {
  110. $columns[] = $att;
  111. }
  112. }
  113. return $columns;
  114. }
  115. //misc
  116. public static function IsColumn($value)
  117. {
  118. if (strlen($value) > 2)
  119. {
  120. if (substr($value, 0, 1) == '`' && substr($value, strlen($value) - 1, 1) == '`')
  121. {
  122. return true;
  123. }
  124. return false;
  125. }
  126. return false;
  127. }
  128. }
  129. ?>