/public/javascripts/tiny_mce/plugins/filemanager/plugins/Favorites/Favorites.php

https://github.com/mahhek/e_champ · PHP · 294 lines · 196 code · 68 blank · 30 comment · 42 complexity · 46f9b29ba303a5998c70acf38bc91fb0 MD5 · raw file

  1. <?php
  2. /**
  3. * FavoriteCookiePlugin.php
  4. *
  5. * @package FavoriteCookiePlugin
  6. * @author Moxiecode
  7. * @copyright Copyright © 2007, Moxiecode Systems AB, All rights reserved.
  8. */
  9. /**
  10. * This class handles MCImageManager FavoriteCookiePlugin stuff.
  11. *
  12. * @package FavoriteCookiePlugin
  13. */
  14. class Moxiecode_FavoritesPlugin extends Moxiecode_ManagerPlugin {
  15. /**#@+
  16. * @access public
  17. */
  18. var $_maxfavorites = 10;
  19. var $_cookieData = null;
  20. /**
  21. * ..
  22. */
  23. function Moxiecode_FavoritePlugin() {
  24. }
  25. function onInit(&$man) {
  26. $man->registerFileSystem('favorite', 'FavoriteFile');
  27. return true;
  28. }
  29. function onRPC(&$man, $cmd, $input) {
  30. if ($cmd == "addFavorites")
  31. return $this->_addFavorites($man, $input);
  32. if ($cmd == "removeFavorites")
  33. return $this->_removeFavorites($man, $input);
  34. return null;
  35. }
  36. function _removeFavorites(&$man, $input) {
  37. $result = new Moxiecode_ResultSet("status,file,message");
  38. for ($i=0; isset($input['path' . $i]); $i++) {
  39. $status = $this->removeFavorite($man, $input["path". $i]);
  40. if ($status)
  41. $result->add("OK", $man->encryptPath($input["path". $i]), "Path was removed.");
  42. else
  43. $result->add("FAILED", $man->encryptPath($input["path". $i]), "Path was not removed.");
  44. }
  45. return $result->toArray();
  46. }
  47. function _addFavorites(&$man, $input) {
  48. $result = new Moxiecode_ResultSet("status,file,message");
  49. for ($i=0; isset($input['path' . $i]); $i++) {
  50. $status = $this->addFavorite($man, $input["path". $i]);
  51. if ($status)
  52. $result->add("OK", $man->encryptPath($input["path". $i]), "Path was added.");
  53. else
  54. $result->add("FAILED", $man->encryptPath($input["path". $i]), "Path was not added.");
  55. }
  56. return $result->toArray();
  57. }
  58. function addFavorite(&$man, $path) {
  59. $config = $man->getConfig();
  60. $maxfavorites = isset($config["favorites.max"]) ? $config["favorites.max"] : $this->_maxfavorites;
  61. $type = $man->getType();
  62. $cookievalue = $this->getCookieData($type);
  63. $patharray = array();
  64. $patharray = split(",", $cookievalue);
  65. if (count($patharray) > 0) {
  66. for($i=0;$i<count($patharray);$i++) {
  67. if ($patharray[$i] == $path) {
  68. array_splice($patharray, $i, 1);
  69. break;
  70. }
  71. }
  72. array_unshift($patharray, $path);
  73. if (count($patharray) > $maxfavorites)
  74. array_pop($patharray);
  75. } else
  76. $patharray[] = $path;
  77. $cookievalue = implode(",", $patharray);
  78. $this->setCookieData($type, $cookievalue);
  79. return true;
  80. }
  81. function getCookieData($type) {
  82. if ($this->_cookieData)
  83. return $this->_cookieData;
  84. if (isset($_COOKIE["MCManagerFavoriteCookie_". $type]))
  85. return $_COOKIE["MCManagerFavoriteCookie_". $type];
  86. else
  87. return "";
  88. }
  89. function setCookieData($type, $val) {
  90. $this->_cookieData = $val;
  91. setcookie("MCManagerFavoriteCookie_". $type, $val, time()+(3600*24*30)); // 30 days
  92. }
  93. function clearFavorites(&$man) {
  94. setcookie ("MCManagerFavoriteCookie_". $man->getType(), "", time() - 3600); // 1 hour ago
  95. return true;
  96. }
  97. function removeFavorite(&$man, $path=array()) {
  98. $type = $man->getType();
  99. $cookievalue = $this->getCookieData($type);
  100. $patharray = array();
  101. $patharray = split(",", $cookievalue);
  102. $break = false;
  103. if (count($patharray) > 0) {
  104. for($i=0;$i<count($patharray);$i++) {
  105. if (is_array($path)) {
  106. if (in_array($patharray[$i], $path))
  107. $break = true;
  108. } else {
  109. if ($patharray[$i] == $path)
  110. $break = true;
  111. }
  112. if ($break) {
  113. array_splice($patharray, $i, 1);
  114. break;
  115. }
  116. }
  117. }
  118. $cookievalue = implode(",", $patharray);
  119. $this->setCookieData($type, $cookievalue);
  120. return true;
  121. }
  122. }
  123. class FavoriteFile extends Moxiecode_BaseFileImpl {
  124. function FavoriteFile(&$manager, $absolute_path, $child_name = "", $type = MC_IS_FILE) {
  125. $absolute_path = str_replace('favorite://', '', $absolute_path);
  126. Moxiecode_BaseFileImpl::Moxiecode_BaseFileImpl($manager, $absolute_path, $child_name, $type);
  127. }
  128. function canRead() {
  129. return true;
  130. }
  131. function canWrite() {
  132. return false;
  133. }
  134. function exists() {
  135. return true;
  136. }
  137. function isDirectory() {
  138. return true;
  139. }
  140. function isFile() {
  141. return false;
  142. }
  143. function getParent() {
  144. return null;
  145. }
  146. function getParentFile() {
  147. return null;
  148. }
  149. /**
  150. * Returns an array of File instances.
  151. *
  152. * @return Array array of File instances.
  153. */
  154. function &listFiles() {
  155. $files = $this->listFilesFiltered(new DummyFileFilter());
  156. return $files;
  157. }
  158. /**
  159. * Returns an array of MCE_File instances based on the specified filter instance.
  160. *
  161. * @param MCE_FileFilter &$filter MCE_FileFilter instance to filter files by.
  162. * @return Array array of MCE_File instances based on the specified filter instance.
  163. */
  164. function listFilesFiltered(&$filter) {
  165. $files = array();
  166. $man = $this->_manager;
  167. $type = $man->getType();
  168. $cookievalue = $this->_getCookieData($type);
  169. $patharray = array();
  170. if (IndexOf($cookievalue, ",") != -1)
  171. $patharray = split(",", $cookievalue);
  172. else if ($cookievalue != "")
  173. $patharray[] = $cookievalue;
  174. foreach ($patharray as $path) {
  175. if (!$man->verifyPath($path))
  176. continue;
  177. $file = $man->getFile($path);
  178. if (!$file->exists()) {
  179. $this->_removeFavorite($man, $path);
  180. continue;
  181. }
  182. if ($man->verifyFile($file) < 0)
  183. continue;
  184. if ($filter->accept($file) == BASIC_FILEFILTER_ACCEPTED)
  185. $files[] = $file;
  186. }
  187. return $files;
  188. }
  189. function _getCookieData($type) {
  190. if (isset($_COOKIE["MCManagerFavoriteCookie_". $type]))
  191. return $_COOKIE["MCManagerFavoriteCookie_". $type];
  192. else
  193. return "";
  194. }
  195. function _removeFavorite(&$man, $path=array()) {
  196. $type = $man->getType();
  197. $cookievalue = $this->_getCookieData($type);
  198. $patharray = array();
  199. $patharray = split(",", $cookievalue);
  200. $break = false;
  201. if (count($patharray) > 0) {
  202. for($i=0;$i<count($patharray);$i++) {
  203. if (is_array($path)) {
  204. if (in_array($patharray[$i], $path))
  205. $break = true;
  206. } else {
  207. if ($patharray[$i] == $path)
  208. $break = true;
  209. }
  210. if ($break) {
  211. array_splice($patharray, $i, 1);
  212. break;
  213. }
  214. }
  215. }
  216. $cookievalue = implode(",", $patharray);
  217. $this->_setCookieData($type, $cookievalue);
  218. return true;
  219. }
  220. function _setCookieData($type, $val) {
  221. setcookie("MCManagerFavoriteCookie_". $type, $val, time() + (3600 * 24 * 30)); // 30 days
  222. }
  223. }
  224. // Add plugin to MCManager
  225. $man->registerPlugin("favorites", new Moxiecode_FavoritesPlugin());
  226. ?>