Security update for permissions_by_term
[yaffs-website] / vendor / drupal / drupal-driver / src / Drupal / Driver / Cores / Drupal6.php
1 <?php
2
3 namespace Drupal\Driver\Cores;
4
5 use Drupal\Driver\Exception\BootstrapException;
6
7 /**
8  * Drupal 6 core.
9  */
10 class Drupal6 extends AbstractCore {
11
12   /**
13    * The available permissions.
14    *
15    * @var array
16    */
17   protected $availablePermissons;
18
19   /**
20    * {@inheritdoc}
21    */
22   public function bootstrap() {
23     // Validate, and prepare environment for Drupal bootstrap.
24     if (!defined('DRUPAL_ROOT')) {
25       define('DRUPAL_ROOT', $this->drupalRoot);
26       require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
27       $this->validateDrupalSite();
28     }
29
30     // Bootstrap Drupal.
31     $current_path = getcwd();
32     chdir(DRUPAL_ROOT);
33     drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
34     if (empty($GLOBALS['db_url'])) {
35       throw new BootstrapException('Missing database setting, verify the database configuration in settings.php.');
36     }
37     drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
38     chdir($current_path);
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function clearCache() {
45     // Need to change into the Drupal root directory or the registry explodes.
46     $current_path = getcwd();
47     chdir(DRUPAL_ROOT);
48     drupal_flush_all_caches();
49     chdir($current_path);
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function nodeCreate($node) {
56     $current_path = getcwd();
57     chdir(DRUPAL_ROOT);
58
59     // Set original if not set.
60     if (!isset($node->original)) {
61       $node->original = clone $node;
62     }
63
64     // Assign authorship if none exists and `author` is passed.
65     if (!isset($node->uid) && !empty($node->author) && ($user = user_load(array('name' => $node->author)))) {
66       $node->uid = $user->uid;
67     }
68
69     // Convert properties to expected structure.
70     $this->expandEntityProperties($node);
71
72     // Attempt to decipher any fields that may be specified.
73     $this->expandEntityFields('node', $node);
74
75     // Set defaults that haven't already been set.
76     $defaults = clone $node;
77     module_load_include('inc', 'node', 'node.pages');
78     node_object_prepare($defaults);
79     $node = (object) array_merge((array) $defaults, (array) $node);
80
81     node_save($node);
82
83     chdir($current_path);
84     return $node;
85
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function nodeDelete($node) {
92     node_delete($node->nid);
93   }
94
95   /**
96    * Implements CoreInterface::runCron().
97    */
98   public function runCron() {
99     return drupal_cron_run();
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function userCreate(\stdClass $user) {
106     // Default status to TRUE if not explicitly creating a blocked user.
107     if (!isset($user->status)) {
108       $user->status = 1;
109     }
110
111     // Clone user object, otherwise user_save() changes the password to the
112     // hashed password.
113     $account = clone $user;
114     // Convert role array to a keyed array.
115     if (isset($user->roles)) {
116       $roles = array();
117       foreach ($user->roles as $rid) {
118         $roles[$rid] = $rid;
119       }
120       $user->roles = $roles;
121     }
122     $account = user_save((array) $account, (array) $account);
123     // Store the UID.
124     $user->uid = $account->uid;
125     return $user;
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function userDelete(\stdClass $user) {
132     $current_path = getcwd();
133     chdir(DRUPAL_ROOT);
134     user_delete((array) $user, $user->uid);
135     chdir($current_path);
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   public function processBatch() {
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public function userAddRole(\stdClass $user, $role_name) {
148     $roles = array_flip(user_roles());
149     $role = $roles[$role_name];
150     if (!$role) {
151       throw new \RuntimeException(sprintf('No role "%s" exists.', $role_name));
152     }
153     user_multiple_role_edit(array($user->uid), 'add_role', $role);
154   }
155
156   /**
157    * Fetches a user role by role name.
158    *
159    * @param string $role_name
160    *   A string representing the role name.
161    *
162    * @return object
163    *   A fully-loaded role object if a role with the given name exists, or FALSE
164    *   otherwise.
165    *
166    * @see user_role_load()
167    */
168   protected function userRoleLoadByName($role_name) {
169     $result = db_query('SELECT * FROM {role} WHERE name = "%s"', $role_name);
170     return db_fetch_object($result);
171   }
172
173   /**
174    * Check to make sure that the array of permissions are valid.
175    *
176    * @param array $permissions
177    *   Permissions to check.
178    * @param bool $reset
179    *   Reset cached available permissions.
180    *
181    * @return bool
182    *   TRUE or FALSE depending on whether the permissions are valid.
183    */
184   protected function checkPermissions(array $permissions, $reset = FALSE) {
185
186     if (!isset($this->availablePermissons) || $reset) {
187       $this->availablePermissons = array_keys(module_invoke_all('permission'));
188     }
189
190     $valid = TRUE;
191     foreach ($permissions as $permission) {
192       if (!in_array($permission, $this->availablePermissons)) {
193         $valid = FALSE;
194       }
195     }
196     return $valid;
197   }
198
199   /**
200    * {@inheritdoc}
201    */
202   public function roleCreate(array $permissions) {
203     // Verify permissions exist.
204     $all_permissions = module_invoke_all('perm');
205     foreach ($permissions as $name) {
206       $search = array_search($name, $all_permissions);
207       if (!$search) {
208         throw new \RuntimeException(sprintf("No permission '%s' exists.", $name));
209       }
210     }
211     // Create new role.
212     $name = $this->random->name(8);
213     db_query("INSERT INTO {role} SET name = '%s'", $name);
214     // Add permissions to role.
215     $rid = db_last_insert_id('role', 'rid');
216     db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', $permissions));
217     return $name;
218   }
219
220   /**
221    * {@inheritdoc}
222    */
223   public function roleDelete($role_name) {
224     $roles = array_flip(user_roles());
225     $rid = $roles[$role_name];
226     db_query('DELETE FROM {role} WHERE rid = %d', $rid);
227     if (!db_affected_rows()) {
228       throw new \RuntimeException(sprintf('No role "%s" exists.', $rid));
229     }
230   }
231
232   /**
233    * {@inheritdoc}
234    */
235   public function validateDrupalSite() {
236     if ('default' !== $this->uri) {
237       // Fake the necessary HTTP headers that Drupal needs:
238       $drupal_base_url = parse_url($this->uri);
239       // If there's no url scheme set, add http:// and re-parse the url
240       // so the host and path values are set accurately.
241       if (!array_key_exists('scheme', $drupal_base_url)) {
242         $drupal_base_url = parse_url($this->uri);
243       }
244       // Fill in defaults.
245       $drupal_base_url += array(
246         'path' => NULL,
247         'host' => NULL,
248         'port' => NULL,
249       );
250       $_SERVER['HTTP_HOST'] = $drupal_base_url['host'];
251
252       if ($drupal_base_url['port']) {
253         $_SERVER['HTTP_HOST'] .= ':' . $drupal_base_url['port'];
254       }
255       $_SERVER['SERVER_PORT'] = $drupal_base_url['port'];
256
257       if (array_key_exists('path', $drupal_base_url)) {
258         $_SERVER['PHP_SELF'] = $drupal_base_url['path'] . '/index.php';
259       }
260       else {
261         $_SERVER['PHP_SELF'] = '/index.php';
262       }
263     }
264     else {
265       $_SERVER['HTTP_HOST'] = 'default';
266       $_SERVER['PHP_SELF'] = '/index.php';
267     }
268
269     $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
270     $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
271     $_SERVER['REQUEST_METHOD']  = NULL;
272
273     $_SERVER['SERVER_SOFTWARE'] = NULL;
274     $_SERVER['HTTP_USER_AGENT'] = NULL;
275
276     $conf_path = conf_path(TRUE, TRUE);
277     $conf_file = $this->drupalRoot . "/$conf_path/settings.php";
278     if (!file_exists($conf_file)) {
279       throw new BootstrapException(sprintf('Could not find a Drupal settings.php file at "%s"', $conf_file));
280     }
281     $drushrc_file = $this->drupalRoot . "/$conf_path/drushrc.php";
282     if (file_exists($drushrc_file)) {
283       require_once $drushrc_file;
284     }
285   }
286
287   /**
288    * Expands properties on the given entity object to the expected structure.
289    *
290    * @param \stdClass $entity
291    *   The entity object.
292    */
293   protected function expandEntityProperties(\stdClass $entity) {
294     // The created field may come in as a readable date, rather than a
295     // timestamp.
296     if (isset($entity->created) && !is_numeric($entity->created)) {
297       $entity->created = strtotime($entity->created);
298     }
299
300     // Map human-readable node types to machine node types.
301     $types = node_get_types();
302     foreach ($types as $type) {
303       if ($entity->type == $type->name) {
304         $entity->type = $type->type;
305         continue;
306       }
307     }
308   }
309
310   /**
311    * Load vocabularies, optional by VIDs.
312    *
313    * @param array $vids
314    *   The vids to load.
315    *
316    * @return array
317    *   An array of vocabulary objects
318    */
319   protected function taxonomyVocabularyLoadMultiple($vids = array()) {
320     $vocabularies = taxonomy_get_vocabularies();
321     if ($vids) {
322       return array_intersect_key($vocabularies, array_flip($vids));
323     }
324     return $vocabularies;
325   }
326
327   /**
328    * {@inheritdoc}
329    */
330   public function termCreate(\stdClass $term) {
331     // Map vocabulary names to vid, these take precedence over machine names.
332     if (!isset($term->vid)) {
333       $vocabularies = \taxonomy_get_vocabularies();
334       foreach ($vocabularies as $vid => $vocabulary) {
335         if ($vocabulary->name == $term->vocabulary_machine_name) {
336           $term->vid = $vocabulary->vid;
337         }
338       }
339     }
340
341     if (!isset($term->vid)) {
342
343       // Try to load vocabulary by machine name.
344       $vocabularies = $this->taxonomyVocabularyLoadMultiple(array($term->vid));
345       if (!empty($vocabularies)) {
346         $vids = array_keys($vocabularies);
347         $term->vid = reset($vids);
348       }
349     }
350
351     // If `parent` is set, look up a term in this vocab with that name.
352     if (isset($term->parent)) {
353       $parent = \taxonomy_get_term_by_name($term->parent);
354       if (!empty($parent)) {
355         $parent = reset($parent);
356         $term->parent = $parent->tid;
357       }
358     }
359
360     if (empty($term->vid)) {
361       throw new \Exception(sprintf('No "%s" vocabulary found.'));
362     }
363
364     // Attempt to decipher any fields that may be specified.
365     $this->expandEntityFields('taxonomy_term', $term);
366
367     // Protect against a failure from hook_taxonomy_term_insert() in pathauto.
368     $current_path = getcwd();
369     chdir(DRUPAL_ROOT);
370     $term_array = (array) $term;
371     \taxonomy_save_term($term_array);
372     chdir($current_path);
373
374     // Loading a term by name returns an array of term objects, but there should
375     // only be one matching term in a testing context, so take the first match
376     // by reset()'ing $matches.
377     $matches = \taxonomy_get_term_by_name($term->name);
378     $saved_term = reset($matches);
379
380     return $saved_term;
381   }
382
383   /**
384    * {@inheritdoc}
385    */
386   public function termDelete(\stdClass $term) {
387     $status = 0;
388     if (isset($term->tid)) {
389       $status = \taxonomy_del_term($term->tid);
390     }
391     // Will be SAVED_DELETED (3) on success.
392     return $status;
393   }
394
395   /**
396    * Helper function to get all permissions.
397    *
398    * @return array
399    *   Array keyed by permission name, with the human-readable title as the
400    *   value.
401    */
402   protected function getAllPermissions() {
403     $permissions = array();
404     foreach (module_invoke_all('permission') as $name => $permission) {
405       $permissions[$name] = $permission['title'];
406     }
407     return $permissions;
408   }
409
410   /**
411    * {@inheritdoc}
412    */
413   public function getModuleList() {
414     return module_list();
415   }
416
417   /**
418    * {@inheritdoc}
419    */
420   public function getExtensionPathList() {
421     $paths = array();
422
423     // Get enabled modules.
424     $modules = $this->getModuleList();
425     foreach ($modules as $module) {
426       $paths[] = $this->drupalRoot . DIRECTORY_SEPARATOR . \drupal_get_path('module', $module);
427     }
428
429     return $paths;
430   }
431
432   /**
433    * {@inheritdoc}
434    */
435   protected function expandEntityFields($entity_type, \stdClass $entity) {
436     return parent::expandEntityFields($entity_type, $entity);
437   }
438
439   /**
440    * {@inheritdoc}
441    */
442   public function getEntityFieldTypes($entity_type) {
443     $taxonomy_fields = array('taxonomy' => 'taxonomy');
444     if (!module_exists('content')) {
445       return $taxonomy_fields;
446     }
447     $return = array();
448     $fields = content_fields();
449     foreach ($fields as $field_name => $field) {
450       if ($this->isField($entity_type, $field_name)) {
451         $return[$field_name] = $field['type'];
452       }
453     }
454
455     $return += $taxonomy_fields;
456
457     return $return;
458   }
459
460   /**
461    * {@inheritdoc}
462    */
463   public function isField($entity_type, $field_name) {
464     if ($field_name === 'taxonomy') {
465       return TRUE;
466     }
467     if (!module_exists('content')) {
468       return FALSE;
469     }
470     $map = content_fields();
471     return isset($map[$field_name]);
472   }
473
474   /**
475    * {@inheritdoc}
476    */
477   public function languageCreate(\stdClass $language) {
478     throw new \Exception('Creating languages is not yet implemented for Drupal 6.');
479   }
480
481   /**
482    * {@inheritdoc}
483    */
484   public function languageDelete(\stdClass $language) {
485     throw new \Exception('Deleting languages is not yet implemented for Drupal 6.');
486   }
487
488   /**
489    * {@inheritdoc}
490    */
491   public function configGet($name, $key = '') {
492     throw new \Exception('Getting config is not yet implemented for Drupal 6.');
493   }
494
495   /**
496    * {@inheritdoc}
497    */
498   public function configSet($name, $key, $value) {
499     throw new \Exception('Setting config is not yet implemented for Drupal 6.');
500   }
501
502   /**
503    * {@inheritdoc}
504    */
505   public function clearStaticCaches() {
506     // Drupal 6 doesn't have a way of clearing all static caches.
507   }
508
509 }