function create_programming_language($pl_data, $existing_id = null) { if (empty($pl_data)) { throw new Exception('-'); } $this->conn->beginTransaction(); if ($existing_id) { // ensure that it exists $sql = 'SELECT COUNT(*) AS n FROM `programming_language` WHERE programming_language_id = ?;'; $s = $this->conn->prepare($sql); if (!$s) { $this->conn->rollBack(); throw new DatabaseException($this->conn->errorInfo()[2]); } if (!$s->execute(array($existing_id))) { $this->conn->rollBack(); throw new DatabaseException($s->errorInfo()[2]); } $n = $s->fetch(PDO::FETCH_ASSOC)['n']; if ($n < 1) { $this->conn->rollBack(); return false; } $sql = 'DELETE FROM `compiler_flag` WHERE programming_language_id = ?;'; $s = $this->conn->prepare($sql); if (!$s) { $this->conn->rollBack(); throw new DatabaseException($this->conn->errorInfo()[2]); } if (!$s->execute(array($existing_id))) { $this->conn->rollBack(); throw new DatabaseException($s->errorInfo()[2]); } } $fields = array('display_name', 'default_extension', 'editor_mode', 'check_command', 'compile_command', 'run_command'); foreach ($fields as $f) { set_empty_if_undefined($pl_data[$f]); if (!is_string($pl_data[$f])) { throw new Exception(); } $pl_data[$f] = trim($pl_data[$f]); } $pl_data['default_extension'] = trim(preg_replace('/^\\.+/', '', trim($pl_data['default_extension']))); $err = array(); $this->msg->set_root('/new-pl/errors'); if (!Validator::validate_pl_display_name($pl_data['display_name'])) { $err['display_name'] = $this->msg->_('display_name', array(1, 32)); } if (!Validator::validate_pl_extension($pl_data['default_extension'])) { $err['default_extension'] = $this->msg->_('default_extension', array(1, 7)); } if (!Validator::validate_pl_editor_mode($pl_data['editor_mode'])) { $err['editor_mode'] = $this->msg->_('editor_mode', array(31)); } if (!Validator::validate_pl_command($pl_data['check_command'], false)) { $err['check_command'] = $this->msg->_('check_command', array(127)); } if (!Validator::validate_pl_command($pl_data['compile_command'], false)) { $err['compile_command'] = $this->msg->_('compile_command', array(127)); } if (!Validator::validate_pl_command($pl_data['run_command'], true)) { $err['run_command'] = $this->msg->_('run_command', array(1, 127)); } if (!empty($err)) { $this->conn->rollBack(); throw new Exception(my_json_encode($err)); } if (!$existing_id) { $sql1 = 'INSERT IGNORE INTO `programming_language` (display_name, default_extension, editor_mode, check_command, compile_command, run_command) VALUES (:display_name, :default_extension, :editor_mode, :check_command, :compile_command, :run_command);'; } else { $sql1 = 'UPDATE `programming_language` SET display_name = :display_name, default_extension = :default_extension, editor_mode = :editor_mode, check_command = :check_command, compile_command = :compile_command, run_command = :run_command WHERE programming_language_id = :pl_id;'; } $s = $this->conn->prepare($sql1); if (!$s) { throw new DatabaseException($this->conn->errorInfo()[2]); $this->conn->rollBack(); } $s->bindValue(':display_name', $pl_data['display_name']); $s->bindValue(':default_extension', $pl_data['default_extension']); $s->bindValue(':editor_mode', null_if_empty_string($pl_data['editor_mode'])); $s->bindValue(':check_command', null_if_empty_string($pl_data['check_command'])); $s->bindValue(':compile_command', null_if_empty_string($pl_data['compile_command'])); $s->bindValue(':run_command', $pl_data['run_command']); if ($existing_id) { $s->bindValue(':pl_id', $existing_id); } if (!$s->execute()) { $this->conn->rollBack(); throw new DatabaseException($s->errorInfo()[2]); } if (!$existing_id and $s->rowCount() < 1) { $this->conn->rollBack(); $err[] = array('display_name' => $this->msg->_('display_name.exists')); throw new ConflictException(my_json_encode($err)); } $lid = $existing_id ? $existing_id : $this->conn->lastInsertId(); if (!isset($pl_data['compiler_flags'])) { $pl_data['compiler_flags'] = array(); } $sql2 = 'INSERT INTO `compiler_flag` (flag, description, programming_language_id, enabled_by_default) VALUES '; $sql2_items = array(); $arguments2 = array(); foreach ($pl_data['compiler_flags'] as $cf) { set_empty_if_undefined($cf['flag']); set_empty_if_undefined($cf['description']); if (!isset($cf['enabled_by_default'])) { $cf['enabled_by_default'] = false; } if (!Validator::validate_pl_flag($cf['flag'])) { $err['flag'] = $this->msg->_('flag', [1, 31]); } if (!Validator::validate_boolean($cf['enabled_by_default'])) { $l = array(1, 31); $err['enabled_by_default'] = $this->msg->_('enabled_by_default', $l); } if (!Validator::validate_pl_flag_description($cf['description'])) { $err['description'] = $this->msg->_('flag_description', [63]); } $sql2_items[] = ' (?, ?, ?, ?)'; $arguments2[] = $cf['flag']; $arguments2[] = null_if_empty_string($cf['description']); $arguments2[] = $lid; $arguments2[] = (int) $cf['enabled_by_default']; } if (!empty($err)) { $this->conn->rollBack(); throw new Exception(my_json_encode($err)); } if ($arguments2) { $sql2 .= implode(', ', $sql2_items) . ';'; $s = $this->conn->prepare($sql2); if (!$s) { $this->conn->rollBack(); throw new DatabaseException($this->conn->errorInfo()[2]); } if (!$s->execute($arguments2)) { $this->conn->rollBack(); throw new DatabaseException($s->errorInfo()[2]); } } $this->conn->commit(); return $lid; }
<?php function null_if_empty_string($string) { if (0 === strlen(trim($string))) { return null; } return $string; } if (null === $recent_posts) { $recent_posts = array(); } foreach ($recent_posts as $key => $post) { $recent_posts[$key]['created'] = date(DateTime::ISO8601, strtotime($post['created'])); } $user_profile = array('id' => (int) $user_data->id, 'username' => $user_data->username, 'created' => date(DateTime::ISO8601, strtotime($user_data->created)), 'online_status' => strtolower(str_replace(' ', '_', $user_data->online_status)), 'last_login' => null === $user_data->last_login ? null : date(DateTime::ISO8601, strtotime($user_data->last_login)), 'buddy_count' => $buddy_count, 'threads_count' => $user_data->threads_count, 'comments_count' => $user_data->comments_count, 'average_posts_per_day' => $user_data->average_posts, 'about' => null_if_empty_string($user_data->about_blurb), 'name' => null_if_empty_string($user_data->name), 'location' => null_if_empty_string($user_data->location), 'website_1' => null_if_empty_string($user_data->website_1), 'website_2' => null_if_empty_string($user_data->website_2), 'website_3' => null_if_empty_string($user_data->website_3), 'aim' => null_if_empty_string($user_data->aim), 'delicious_username' => null_if_empty_string($user_data->delicious_username), 'facebook' => null_if_empty_string($user_data->facebook), 'flickr_username' => null_if_empty_string($user_data->flickr_username), 'gchat' => null_if_empty_string($user_data->gchat), 'lastfm' => null_if_empty_string($user_data->lastfm), 'msn' => null_if_empty_string($user_data->msn), 'twitter' => null_if_empty_string($user_data->twitter), 'rss_feed_1' => null_if_empty_string($user_data->rss_feed_1), 'rss_feed_2' => null_if_empty_string($user_data->rss_feed_2), 'rss_feed_3' => null_if_empty_string($user_data->rss_feed_3), 'recent_posts' => $recent_posts); ?> <?php echo json_encode($user_profile);