PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/umsp/funcs-misc.php

http://umspx.googlecode.com/
PHP | 198 lines | 171 code | 14 blank | 13 comment | 34 complexity | 2c71413e6e67ad5eb06f1f00c010e11a MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. include_once($_SERVER["DOCUMENT_ROOT"].'/umsp/funcs-config.php');
  3. function _callPlugin($prmPath, $prmQuery = null) {
  4. # TODO: make plugins config Ini based
  5. # prmPath is expected as '/pluginName'
  6. $tmpArr = explode('/', $prmPath, 3);
  7. if ($tmpArr[1] != '') {
  8. $pluginFile = _getPluginFile($tmpArr[1]);
  9. if (is_null($pluginFile)) {
  10. return NULL;
  11. };
  12. };
  13. # Save the plugin path to tmp to know our location in case of a search query
  14. _writeTmpVar('lastUsedPlugin', $pluginFile);
  15. # Load plugin file and call its _pluginMain function with query string as parameter
  16. # The plugin is expected to return an array of media items
  17. require $pluginFile;
  18. $pluginItems = _pluginMain($prmQuery);
  19. return $pluginItems;
  20. } # end function
  21. function _getPluginFile($pluginName) {
  22. $tmpPluginPath = Array(
  23. dirname(__FILE__).'/user-plugins/'. $pluginName . '.php',
  24. dirname(__FILE__).'/plugins/'. $pluginName . '.php',
  25. dirname(__FILE__).'/plugins/'. $pluginName . '/' . $tmpArr[2] . '.php',
  26. dirname(__FILE__).'/plugins/'. $pluginName . '/' . $pluginName . '.php'
  27. );
  28. foreach ($tmpPluginPath as $key=>$val) {
  29. if (is_file($val)) {
  30. return $val;
  31. };
  32. };
  33. return NULL;
  34. };
  35. function _callPluginSearch($prmQuery) {
  36. $lastPlugin = _readTmpVar('lastUsedPlugin');
  37. if (!is_readable($lastPlugin)){
  38. return NULL;
  39. } else {
  40. require $lastPlugin;
  41. if (function_exists('_pluginSearch')) {
  42. $searchItems = _pluginSearch($prmQuery);
  43. return $searchItems;
  44. } else {
  45. return NULL;
  46. };
  47. };
  48. };
  49. function _createMessageItems($prmText) {
  50. # $prmText can be a single string or an array of multiple strings (lines)
  51. if (is_string($prmText)) { $prmText = array($prmText); }
  52. foreach ($prmText as $line) {
  53. if (trim($line) != '') {
  54. $retItems[] = array('id' => 'msg_' . count($retItems),
  55. 'dc:title' => $line,
  56. 'upnp:class'=> 'object.container',
  57. );
  58. } # end if
  59. } # end foreach
  60. return $retItems;
  61. } # end function
  62. function _readCache($prmId) {
  63. $filename = _getUMSPTmpPath() . '/umsp-cache';
  64. if ($prmId == '0') {
  65. # don't cache root and delete old cache
  66. if (is_file($filename)) {
  67. unlink($filename);
  68. }
  69. return NULL;
  70. } # end if
  71. if (!is_readable($filename)) {
  72. return NULL;
  73. }
  74. $rawContent = file_get_contents($filename);
  75. $cacheData = unserialize($rawContent);
  76. if ((is_array($cacheData['data'])) && ($cacheData['id'] == $prmId)) {
  77. return $cacheData['data'];
  78. } else {
  79. return NULL;
  80. } # end if
  81. } # end function
  82. function _writeCache($prmId, $prmDataArray) {
  83. $filename = _getUMSPTmpPath() . '/umsp-cache';
  84. if (!is_array($prmDataArray)) {
  85. return FALSE;
  86. };
  87. if ($prmId == '0') { # don't cache root
  88. return FALSE;
  89. };
  90. $rawContent = serialize(array('id' => $prmId, 'data' => $prmDataArray));
  91. return file_put_contents($filename, $rawContent);
  92. } # end function
  93. function _clearCache() {
  94. $filename = _getUMSPTmpPath() . '/umsp-cache';
  95. return file_put_contents($filename, "");
  96. } # end function
  97. function _readTmpVar($prmVarName) {
  98. $filename = _getUMSPTmpPath() . '/umsp-tmpvars';
  99. if (!is_readable($filename)) {
  100. return NULL;
  101. };
  102. $rawContent = file_get_contents($filename);
  103. $varData = unserialize($rawContent);
  104. if (is_array($varData) && isset($varData[$prmVarName])) {
  105. return $varData[$prmVarName];
  106. } else {
  107. return NULL;
  108. } # end if
  109. } # end function
  110. function _writeTmpVar($prmVarName, $prmVarValue) {
  111. $filename = _getUMSPTmpPath() . '/umsp-tmpvars';
  112. if (is_readable($filename)) {
  113. $rawContent = file_get_contents($filename);
  114. $prevData = unserialize($rawContent);
  115. if ($prevData === FALSE || !is_array($prevData)) {
  116. $newData = Array();
  117. } else {
  118. $newData = $prevData;
  119. }
  120. } else {
  121. $newData = Array();
  122. }
  123. $newData[$prmVarName] = $prmVarValue;
  124. $rawContent = serialize($newData);
  125. return file_put_contents($filename, $rawContent);
  126. } # end function
  127. function _var_dump_pre($mixed = NULL) {
  128. echo '<pre>';
  129. var_dump($mixed);
  130. echo '</pre>';
  131. return NULL;
  132. }
  133. function _var_dump_ret($mixed = NULL) {
  134. ob_start();
  135. var_dump($mixed);
  136. $content = ob_get_contents();
  137. ob_end_clean();
  138. return $content;
  139. }
  140. function _var_dump_to_file($mixed = NULL, $prmFile = '/tmp/umsp-log.txt') {
  141. ob_start();
  142. var_dump($mixed);
  143. $content = ob_get_contents();
  144. ob_end_clean();
  145. $prmFile = _getUMSPTmpPath() . '/umsp-log.txt';
  146. $myFile = $prmFile;
  147. $fh = fopen($myFile, 'a') or die();
  148. fwrite($fh, $content . "\r\n");
  149. fclose($fh);
  150. }
  151. function _my_parse_ini_string($prmIniString) {
  152. $lines = explode("\n", $prmIniString);
  153. $currentSection = NULL;
  154. foreach ($lines as $line) {
  155. $line = trim($line);
  156. $firstChar = substr($line, 0, 1);
  157. switch ($firstChar) {
  158. case ';':
  159. case '#':
  160. # is comment
  161. break;
  162. case '[':
  163. $currentSection = substr($line, 1, -1);
  164. $data[$currentSection] = array();
  165. break;
  166. default:
  167. //skip line feeds in INI file
  168. if (empty($line)) {
  169. continue;
  170. }
  171. //if $currentsection is still null,
  172. //there was missing a "[<sectionName>]"
  173. //before the first key/value pair
  174. if (null === $currentSection) {
  175. return FALSE;
  176. }
  177. //get key and value
  178. list($key, $val) = explode('=', $line, 2);
  179. $data[$currentSection][$key] = $val;
  180. } # end switch
  181. } # end foreach
  182. return $data;
  183. } # end function
  184. ?>