modmediasource.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * The interface used for defining common methods for Media Source drivers
  12. * @package modx
  13. */
  14. interface modMediaSourceInterface
  15. /**
  16. * Initialize the source, preparing it for usage.
  17. *
  18. * @return boolean
  19. */{
  20. public function initialize();
  21. /**
  22. * Return an array of containers at this current level in the container structure. Used for the tree
  23. * navigation on the files tree.
  24. *
  25. * @abstract
  26. * @param string $path
  27. * @return array
  28. */
  29. public function getContainerList($path);
  30. /**
  31. * Return a detailed list of objects in a specific path. Used for thumbnails in the Browser.
  32. *
  33. * @param string $path
  34. * @return array
  35. */
  36. public function getObjectsInContainer($path);
  37. /**
  38. * Create a container at the passed location with the passed name
  39. *
  40. * @abstract
  41. * @param string $name
  42. * @param string $parentContainer
  43. * @return boolean
  44. */
  45. public function createContainer($name,$parentContainer);
  46. /**
  47. * Remove the specified container
  48. *
  49. * @abstract
  50. * @param string $path
  51. * @return boolean
  52. */
  53. public function removeContainer($path);
  54. /**
  55. * Rename a container
  56. *
  57. * @abstract
  58. * @param string $oldPath
  59. * @param string $newName
  60. * @return boolean
  61. */
  62. public function renameContainer($oldPath,$newName);
  63. /**
  64. * Upload objects to a specific container
  65. *
  66. * @abstract
  67. * @param string $container
  68. * @param array $objects
  69. * @return boolean
  70. */
  71. public function uploadObjectsToContainer($container,array $objects = array());
  72. /**
  73. * Get the contents of an object
  74. *
  75. * @abstract
  76. * @param string $objectPath
  77. * @return boolean
  78. */
  79. public function getObjectContents($objectPath);
  80. /**
  81. * Update the contents of a specific object
  82. *
  83. * @abstract
  84. * @param string $objectPath
  85. * @param string $content
  86. * @return boolean
  87. */
  88. public function updateObject($objectPath,$content);
  89. /**
  90. * Create an object from a path
  91. *
  92. * @param string $objectPath
  93. * @param string $name
  94. * @param string $content
  95. * @return boolean|string
  96. */
  97. public function createObject($objectPath,$name,$content);
  98. /**
  99. * Remove an object
  100. *
  101. * @abstract
  102. * @param string $objectPath
  103. * @return boolean
  104. */
  105. public function removeObject($objectPath);
  106. /**
  107. * Rename a file/object
  108. *
  109. * @abstract
  110. * @param string $oldPath
  111. * @param string $newName
  112. * @return bool
  113. */
  114. public function renameObject($oldPath,$newName);
  115. /**
  116. * Get the openTo path for this source, used with TV input types and Static Elements/Resources
  117. *
  118. * @param string $value
  119. * @param array $parameters
  120. * @return string
  121. */
  122. public function getOpenTo($value,array $parameters = array());
  123. /**
  124. * Get the base path for this source. Only applicable to sources that are streams, used for determining
  125. * the base path with Static objects.
  126. *
  127. * @param string $object An optional file to find the base path with
  128. * @return string
  129. */
  130. public function getBasePath($object = '');
  131. /**
  132. * Get the base URL for this source. Only applicable to sources that are streams; used for determining the base
  133. * URL with Static objects and downloading objects.
  134. *
  135. * @abstract
  136. * @param string $object
  137. * @return void
  138. */
  139. public function getBaseUrl($object = '');
  140. /**
  141. * Get the URL for an object in this source. Only applicable to sources that are streams; used for determining
  142. * the base URL with Static objects and downloading objects.
  143. *
  144. * @abstract
  145. * @param string $object
  146. * @return void
  147. */
  148. public function getObjectUrl($object = '');
  149. /**
  150. * Move a file or folder to a specific location
  151. *
  152. * @param string $from The location to move from
  153. * @param string $to The location to move to
  154. * @param string $point The type of move; append, above, below
  155. * @return boolean
  156. */
  157. public function moveObject($from,$to,$point = 'append');
  158. /**
  159. * Get the name of this source type, ie, "File System"
  160. * @return string
  161. */
  162. public function getTypeName();
  163. /**
  164. * Get a short description of this source type
  165. * @return string
  166. */
  167. public function getTypeDescription();
  168. /**
  169. * Get the default properties for this source. Override this in your custom source driver to provide custom
  170. * properties for your source type.
  171. * @return array
  172. */
  173. public function getDefaultProperties();
  174. }
  175. /**
  176. * An abstract base class used for determining functionality of different media source drivers. Extend this class in
  177. * and implement modMediaSourceInterface in your driver implementations to provide custom functionality for different
  178. * types of media sources.
  179. *
  180. * Of course, in your getContainerList method, you can define the context menu items for the tree, so not all of these
  181. * methods might need to be implemented, depending on your situation. You can also provide custom actions for your
  182. * source type, depending on the behavior you might need.
  183. *
  184. * @package modx
  185. * @subpackage sources
  186. */
  187. class modMediaSource extends modAccessibleSimpleObject implements modMediaSourceInterface {
  188. /** @var modX|xPDO $xpdo */
  189. public $xpdo;
  190. /** @var modContext $ctx */
  191. public $ctx;
  192. /** @var array $properties */
  193. public $properties = array();
  194. /** @var array $permissions */
  195. public $permissions = array();
  196. /** @var array $errors */
  197. public $errors = array();
  198. /**
  199. * Get the default MODX filesystem source
  200. * @static
  201. * @param xPDO|modX $xpdo A reference to an xPDO instance
  202. * @param int $defaultSourceId
  203. * @param boolean $fallbackToDefault
  204. * @return modMediaSource|null
  205. */
  206. public static function getDefaultSource(xPDO &$xpdo,$defaultSourceId = null,$fallbackToDefault = true) {
  207. if (empty($defaultSourceId)) {
  208. $defaultSourceId = $xpdo->getOption('default_media_source',null,1);
  209. }
  210. /** @var modMediaSource $defaultSource */
  211. $defaultSource = $xpdo->getObject('sources.modMediaSource',array(
  212. 'id' => $defaultSourceId,
  213. ));
  214. if (empty($defaultSource) && $fallbackToDefault) {
  215. $c = $xpdo->newQuery('sources.modMediaSource');
  216. $c->sortby('id','ASC');
  217. $defaultSource = $xpdo->getObject('sources.modMediaSource',$c);
  218. }
  219. return $defaultSource;
  220. }
  221. /**
  222. * Get the current working context for the processor
  223. * @return bool|modContext
  224. */
  225. public function getWorkingContext() {
  226. $wctx = isset($this->properties['wctx']) && !empty($this->properties['wctx']) ? $this->properties['wctx'] : '';
  227. if (!empty($wctx)) {
  228. $workingContext = $this->xpdo->getContext($wctx);
  229. if (!$workingContext) {
  230. return false;
  231. }
  232. } else {
  233. $workingContext =& $this->xpdo->context;
  234. }
  235. $this->ctx =& $workingContext;
  236. return $this->ctx;
  237. }
  238. /**
  239. * Initialize the source
  240. * @return boolean
  241. */
  242. public function initialize() {
  243. $this->setProperties($this->getProperties(true));
  244. $this->getPermissions();
  245. return true;
  246. }
  247. /**
  248. * Setup the request properties for the source, determining any request-specific actions
  249. * @param array $scriptProperties
  250. * @return array
  251. */
  252. public function setRequestProperties(array $scriptProperties = array()) {
  253. if (empty($this->properties)) $this->properties = array();
  254. $this->properties = array_merge($this->getPropertyList(),$this->properties,$scriptProperties);
  255. return $this->properties;
  256. }
  257. /**
  258. * Get a list of permissions for browsing and utilizing the source. May be overridden to provide a custom
  259. * list of permissions.
  260. * @return array
  261. */
  262. public function getPermissions() {
  263. $this->permissions = array(
  264. 'directory_chmod' => $this->xpdo->hasPermission('directory_chmod'),
  265. 'directory_create' => $this->xpdo->hasPermission('directory_create'),
  266. 'directory_list' => $this->xpdo->hasPermission('directory_list'),
  267. 'directory_remove' => $this->xpdo->hasPermission('directory_remove'),
  268. 'directory_update' => $this->xpdo->hasPermission('directory_update'),
  269. 'file_list' => $this->xpdo->hasPermission('file_list'),
  270. 'file_remove' => $this->xpdo->hasPermission('file_remove'),
  271. 'file_update' => $this->xpdo->hasPermission('file_update'),
  272. 'file_upload' => $this->xpdo->hasPermission('file_upload'),
  273. 'file_unpack' => $this->xpdo->hasPermission('file_unpack'),
  274. 'file_view' => $this->xpdo->hasPermission('file_view'),
  275. 'file_create' => $this->xpdo->hasPermission('file_create'),
  276. );
  277. return $this->permissions;
  278. }
  279. /**
  280. * See if the source is allowing a certain permission.
  281. *
  282. * @param string $key
  283. * @return bool
  284. */
  285. public function hasPermission($key) {
  286. return !empty($this->permissions[$key]);
  287. }
  288. /**
  289. * Add an error for an action occurring in the source
  290. *
  291. * @param string $field The field corresponding to the error
  292. * @param string $message The message to add
  293. * @return string The added error
  294. */
  295. public function addError($field,$message) {
  296. $this->errors[$field] = $message;
  297. return $message;
  298. }
  299. /**
  300. * Get all errors that have occurred for this source
  301. * @return array
  302. */
  303. public function getErrors() {
  304. return $this->errors;
  305. }
  306. /**
  307. * See if the source has any errors
  308. * @return bool
  309. */
  310. public function hasErrors() {
  311. return !empty($this->errors);
  312. }
  313. public function getContainerList($path) { return array(); }
  314. public function getObjectsInContainer($path) { return array(); }
  315. public function createContainer($name,$parentContainer) { return true; }
  316. public function removeContainer($path) { return true; }
  317. public function renameContainer($oldPath,$newName) { return true; }
  318. public function updateContainer() { return true; }
  319. public function uploadObjectsToContainer($container,array $objects = array()) { return true; }
  320. public function getObjectContents($objectPath) { return true; }
  321. public function updateObject($objectPath,$content) { return true; }
  322. public function createObject($objectPath,$name,$content) { return true; }
  323. public function removeObject($objectPath) { return true; }
  324. public function renameObject($oldPath,$newName) { return true; }
  325. public function getBasePath($object = '') { return ''; }
  326. public function getBaseUrl($object = '') { return ''; }
  327. public function getObjectUrl($object = '') { return ''; }
  328. public function moveObject($from,$to,$point = 'append') { return true; }
  329. public function getDefaultProperties() { return array(); }
  330. /**
  331. * Get the openTo directory for this source, used with TV input types
  332. *
  333. * @param string $value
  334. * @param array $parameters
  335. * @return string
  336. */
  337. public function getOpenTo($value,array $parameters = array()) {
  338. $dirname = dirname($value);
  339. return $dirname == '.' ? '' : $dirname . '/';
  340. }
  341. /**
  342. * Get the name of this source type
  343. * @return string
  344. */
  345. public function getTypeName() {
  346. $this->xpdo->lexicon->load('source');
  347. return $this->xpdo->lexicon('source_type.file');
  348. }
  349. /**
  350. * Get the description of this source type
  351. * @return string
  352. */
  353. public function getTypeDescription() {
  354. $this->xpdo->lexicon->load('source');
  355. return $this->xpdo->lexicon('source_type.file_desc');
  356. }
  357. /**
  358. * Get the properties on this source
  359. * @param boolean $parsed
  360. * @return array
  361. */
  362. public function getProperties($parsed = false) {
  363. $properties = $this->get('properties');
  364. $defaultProperties = $this->getDefaultProperties();
  365. if (!empty($properties) && is_array($properties)) {
  366. foreach ($properties as &$property) {
  367. $property['overridden'] = 0;
  368. if (array_key_exists($property['name'],$defaultProperties)) {
  369. if ($defaultProperties[$property['name']]['value'] != $property['value']) {
  370. $property['overridden'] = 1;
  371. }
  372. } else {
  373. $property['overridden'] = 2;
  374. }
  375. }
  376. $properties = array_merge($defaultProperties,$properties);
  377. } else {
  378. $properties = $defaultProperties;
  379. }
  380. /** @var array $results Allow manipulation of media source properties via event */
  381. $results = $this->xpdo->invokeEvent('OnMediaSourceGetProperties',array(
  382. 'properties' => $this->xpdo->toJSON($properties),
  383. ));
  384. if (!empty($results)) {
  385. foreach ($results as $result) {
  386. $result = is_array($result) ? $result : $this->xpdo->fromJSON($result);
  387. if (!is_array($result)) {
  388. $result = array();
  389. }
  390. $properties = array_merge($properties,$result);
  391. }
  392. }
  393. if ($parsed) {
  394. $properties = $this->parseProperties($properties);
  395. }
  396. return $this->prepareProperties($properties);
  397. }
  398. /**
  399. * Get all properties in key => value format
  400. * @return array
  401. */
  402. public function getPropertyList() {
  403. $properties = $this->getProperties(true);
  404. $list = array();
  405. foreach ($properties as $property) {
  406. $value = $property['value'];
  407. if (!empty($property['xtype']) && $property['xtype'] == 'combo-boolean') {
  408. $value = empty($property['value']) && $property['value'] != 'true' ? false : true;
  409. }
  410. $list[$property['name']] = $value;
  411. }
  412. $list = array_merge($list,$this->properties);
  413. return $list;
  414. }
  415. /**
  416. * Parse any tags in the properties
  417. * @param array $properties
  418. * @return array
  419. */
  420. public function parseProperties(array $properties) {
  421. $properties = $this->getProperties();
  422. $this->xpdo->getParser();
  423. if ($this->xpdo->parser) {
  424. foreach ($properties as &$property) {
  425. $this->xpdo->parser->processElementTags('',$property['value'],true,true);
  426. }
  427. }
  428. return $properties;
  429. }
  430. /**
  431. * Translate any needed properties
  432. * @param array $properties
  433. * @return array
  434. */
  435. public function prepareProperties(array $properties = array()) {
  436. foreach ($properties as &$property) {
  437. if (!empty($property['lexicon'])) {
  438. $this->xpdo->lexicon->load($property['lexicon']);
  439. }
  440. if (!empty($property['name'])) {
  441. $property['name_trans'] = $this->xpdo->lexicon($property['name']);
  442. }
  443. if (!empty($property['desc'])) {
  444. $property['desc_trans'] = $this->xpdo->lexicon($property['desc']);
  445. }
  446. if (!empty($property['options'])) {
  447. foreach ($property['options'] as &$option) {
  448. if (empty($option['text']) && !empty($option['name'])) {
  449. $option['text'] = $option['name'];
  450. unset($option['name']);
  451. }
  452. if (empty($option['value']) && !empty($option[0])) {
  453. $option['value'] = $option[0];
  454. unset($option[0]);
  455. }
  456. $option['name'] = $this->xpdo->lexicon($option['text']);
  457. }
  458. }
  459. }
  460. return $properties;
  461. }
  462. /**
  463. * Set the properties for this Source
  464. *
  465. * @param array $properties
  466. * @param boolean $merge
  467. * @return bool
  468. */
  469. public function setProperties($properties, $merge = false) {
  470. $default = $this->getDefaultProperties();
  471. foreach ($properties as $k => $prop) {
  472. if (is_array($prop) && array_key_exists($prop['name'],$default)) {
  473. if ($prop['value'] == $default[$prop['name']]['value']) {
  474. unset($properties[$k]);
  475. }
  476. } else if (is_scalar($prop)) { /* properties is k=>v pair */
  477. if ($prop == $default[$k]['value']) {
  478. unset($properties[$k]);
  479. }
  480. }
  481. }
  482. $set = false;
  483. $propertiesArray = array();
  484. if (is_string($properties)) {
  485. $properties = $this->xpdo->parser->parsePropertyString($properties);
  486. }
  487. if (is_array($properties)) {
  488. foreach ($properties as $propKey => $property) {
  489. if (is_array($property) && isset($property[5])) {
  490. $key = $property[0];
  491. $propertyArray = array(
  492. 'name' => $property[0],
  493. 'desc' => $property[1],
  494. 'type' => $property[2],
  495. 'options' => $property[3],
  496. 'value' => $property[4],
  497. 'lexicon' => !empty($property[5]) ? $property[5] : null,
  498. );
  499. } elseif (is_array($property) && isset($property['value'])) {
  500. $key = $property['name'];
  501. $propertyArray = array(
  502. 'name' => $property['name'],
  503. 'desc' => isset($property['description']) ? $property['description'] : (isset($property['desc']) ? $property['desc'] : ''),
  504. 'type' => isset($property['xtype']) ? $property['xtype'] : (isset($property['type']) ? $property['type'] : 'textfield'),
  505. 'options' => isset($property['options']) ? $property['options'] : array(),
  506. 'value' => $property['value'],
  507. 'lexicon' => !empty($property['lexicon']) ? $property['lexicon'] : null,
  508. );
  509. } else {
  510. $key = $propKey;
  511. $propertyArray = array(
  512. 'name' => $propKey,
  513. 'desc' => '',
  514. 'type' => 'textfield',
  515. 'options' => array(),
  516. 'value' => $property,
  517. 'lexicon' => null,
  518. );
  519. }
  520. if (!empty($propertyArray['options'])) {
  521. foreach ($propertyArray['options'] as $optionKey => &$option) {
  522. if (empty($option['text']) && !empty($option['name'])) $option['text'] = $option['name'];
  523. unset($option['menu'],$option['name']);
  524. }
  525. }
  526. if ($propertyArray['type'] == 'combo-boolean' && is_numeric($propertyArray['value'])) {
  527. $propertyArray['value'] = (boolean)$propertyArray['value'];
  528. }
  529. $propertiesArray[$key] = $propertyArray;
  530. }
  531. if ($merge && !empty($propertiesArray)) {
  532. $existing = $this->get('properties');
  533. if (is_array($existing) && !empty($existing)) {
  534. $propertiesArray = array_merge($existing, $propertiesArray);
  535. }
  536. }
  537. $set = $this->set('properties', $propertiesArray);
  538. }
  539. return $set;
  540. }
  541. /**
  542. * Prepare the source path for phpThumb
  543. *
  544. * @param string $src
  545. * @return string
  546. */
  547. public function prepareSrcForThumb($src) {
  548. /* dont strip stuff for absolute URLs */
  549. if (substr($src,0,4) != 'http') {
  550. if (strpos($src,'/') !== 0) {
  551. $properties = $this->getPropertyList();
  552. $src = !empty($properties['basePath']) ? $properties['basePath'].$src : $src;
  553. if (!empty($properties['basePathRelative'])) {
  554. $src = $this->ctx->getOption('base_path',null,MODX_BASE_PATH).$src;
  555. }
  556. }
  557. /* strip out double slashes */
  558. $src = str_replace(array('///','//'),'/',$src);
  559. /* check for file existence if local url */
  560. if (strpos($src,'/') !== 0 && empty($src)) {
  561. if (file_exists('/'.$src)) {
  562. $src = '/'.$src;
  563. } else {
  564. return '';
  565. }
  566. }
  567. }
  568. return $src;
  569. }
  570. /**
  571. * Prepares the output URL when the Source is being used in an Element. Can be overridden to provide prefixing/post-
  572. * fixing functionality.
  573. *
  574. * @param string $value
  575. * @return string
  576. */
  577. public function prepareOutputUrl($value) {
  578. return $value;
  579. }
  580. /**
  581. * Find all policies for this object
  582. *
  583. * @param string $context
  584. * @return array
  585. */
  586. public function findPolicy($context = '') {
  587. $policy = array();
  588. $enabled = true;
  589. $context = 'mgr';
  590. if ($context === $this->xpdo->context->get('key')) {
  591. $enabled = (boolean) $this->xpdo->getOption('access_media_source_enabled', null, true);
  592. } elseif ($this->xpdo->getContext($context)) {
  593. $enabled = (boolean) $this->xpdo->contexts[$context]->getOption('access_media_source_enabled', true);
  594. }
  595. if ($enabled) {
  596. if (empty($this->_policies) || !isset($this->_policies[$context])) {
  597. $accessTable = $this->xpdo->getTableName('sources.modAccessMediaSource');
  598. $sourceTable = $this->xpdo->getTableName('sources.modMediaSource');
  599. $policyTable = $this->xpdo->getTableName('modAccessPolicy');
  600. $sql = "SELECT Acl.target, Acl.principal, Acl.authority, Acl.policy, Policy.data FROM {$accessTable} Acl " .
  601. "LEFT JOIN {$policyTable} Policy ON Policy.id = Acl.policy " .
  602. "JOIN {$sourceTable} Source ON Acl.principal_class = 'modUserGroup' " .
  603. "AND (Acl.context_key = :context OR Acl.context_key IS NULL OR Acl.context_key = '') " .
  604. "AND Source.id = Acl.target " .
  605. "WHERE Acl.target = :source " .
  606. "GROUP BY Acl.target, Acl.principal, Acl.authority, Acl.policy";
  607. $bindings = array(
  608. ':source' => $this->get('id'),
  609. ':context' => $context,
  610. );
  611. $query = new xPDOCriteria($this->xpdo, $sql, $bindings);
  612. if ($query->stmt && $query->stmt->execute()) {
  613. while ($row = $query->stmt->fetch(PDO::FETCH_ASSOC)) {
  614. $policy['sources.modAccessMediaSource'][$row['target']][] = array(
  615. 'principal' => $row['principal'],
  616. 'authority' => $row['authority'],
  617. 'policy' => $row['data'] ? $this->xpdo->fromJSON($row['data'], true) : array(),
  618. );
  619. }
  620. }
  621. $this->_policies[$context] = $policy;
  622. } else {
  623. $policy = $this->_policies[$context];
  624. }
  625. }
  626. return $policy;
  627. }
  628. /**
  629. * Allow overriding of checkPolicy to always allow media sources to be loaded
  630. *
  631. * @param string|array $criteria
  632. * @param array $targets
  633. * @param modUser $user
  634. * @return bool
  635. */
  636. public function checkPolicy($criteria, $targets = null, modUser $user = null) {
  637. if ($criteria == 'load') {
  638. $success = true;
  639. } else {
  640. $success = parent::checkPolicy($criteria,$targets,$user);
  641. }
  642. return $success;
  643. }
  644. /**
  645. * Override xPDOObject::save to clear the sources cache on save
  646. *
  647. * @param boolean $cacheFlag
  648. * @return boolean
  649. */
  650. public function save($cacheFlag = null) {
  651. $saved = parent::save($cacheFlag);
  652. if ($saved) {
  653. $this->clearCache();
  654. }
  655. return $saved;
  656. }
  657. /**
  658. * Clear the caches of all sources
  659. * @param array $options
  660. * @return void
  661. */
  662. public function clearCache(array $options = array()) {
  663. /** @var modCacheManager $cacheManager */
  664. $cacheManager = $this->xpdo->getCacheManager();
  665. if (empty($cacheManager)) return;
  666. $c = $this->xpdo->newQuery('modContext');
  667. $c->select($this->xpdo->escape('key'));
  668. $options[xPDO::OPT_CACHE_KEY] = $this->getOption('cache_media_sources_key', $options, 'media_sources');
  669. $options[xPDO::OPT_CACHE_HANDLER] = $this->getOption('cache_media_sources_handler', $options, $this->getOption(xPDO::OPT_CACHE_HANDLER, $options));
  670. $options[xPDO::OPT_CACHE_FORMAT] = (integer) $this->getOption('cache_media_sources_format', $options, $this->getOption(xPDO::OPT_CACHE_FORMAT, $options, xPDOCacheManager::CACHE_PHP));
  671. $options[xPDO::OPT_CACHE_ATTEMPTS] = (integer) $this->getOption('cache_media_sources_attempts', $options, $this->getOption(xPDO::OPT_CACHE_ATTEMPTS, $options, 10));
  672. $options[xPDO::OPT_CACHE_ATTEMPT_DELAY] = (integer) $this->getOption('cache_media_sources_attempt_delay', $options, $this->getOption(xPDO::OPT_CACHE_ATTEMPT_DELAY, $options, 1000));
  673. if ($c->prepare() && $c->stmt->execute()) {
  674. while ($row = $c->stmt->fetch(PDO::FETCH_ASSOC)) {
  675. if ($row && !empty($row['key'])) {
  676. $cacheManager->delete($row['key'].'/source',$options);
  677. }
  678. }
  679. }
  680. }
  681. }