PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/application_functions.php

https://code.google.com/
PHP | 248 lines | 222 code | 20 blank | 6 comment | 17 complexity | f0cf7b0c6b957638f483b7f4963f5a6f MD5 | raw file
  1. <?php
  2. function only_user($level) {
  3. if (!User::is(">=$level"))
  4. access_denied();
  5. }
  6. function post_only_user($level) {
  7. if (Request::$method != 'POST')
  8. return;
  9. if (User::is("<$level"))
  10. access_denied();
  11. }
  12. function render_error($record) {
  13. render('inline', error_messages_for($record), array('status' => 500, 'layout' => 'bare'));
  14. }
  15. function respond_to_success($notice, $redirect_to_params, $options = array()) {
  16. check_array($redirect_to_params);
  17. $extra_api_params = isset($options['api']) ? $options['api'] : array();
  18. switch (Request::$format) {
  19. case 'html':
  20. notice($notice);
  21. call_user_func_array('redirect_to', $redirect_to_params);
  22. break;
  23. case 'json':
  24. render('json', to_json(array_merge($extra_api_params, array('success' => true))));
  25. break;
  26. }
  27. }
  28. function notice($notice){
  29. cookie_put('notice', $notice);
  30. }
  31. function respond_to_error($obj, $redirect_to_params, $options = array()) {
  32. $extra_api_params = isset($options['api']) ? $options['api'] : array();
  33. $status = isset($options['status']) ? $options['status'] : 500;
  34. if (is_object($obj) && is_subclass_of($obj, 'ActiveRecord')) {
  35. $obj = implode(', ', $obj->record_errors->full_messages());
  36. $status = 420;
  37. }
  38. if ($status == 420)
  39. $status = '420 Invalid Record';
  40. elseif ($status == 421)
  41. $status = '421 User Throttled';
  42. elseif ($status == 422)
  43. $status = '422 Locked';
  44. elseif ($status == 423)
  45. $status = '423 Already Exists';
  46. elseif ($status == 424)
  47. $status = '424 Invalid Parameters';
  48. switch (Request::$format) {
  49. case 'html':
  50. notice('Error: '.$obj);
  51. check_array($redirect_to_params);
  52. call_user_func_array('redirect_to', $redirect_to_params);
  53. case 'json':
  54. render('json', to_json(array_merge($extra_api_params, array('success' => false, 'reason' => $obj))), array('status' => $status));
  55. case 'xml':
  56. // fmt.xml {render :xml => extra_api_params.merge(:success => false, :reason => obj).to_xml(:root => "response"), :status => status}
  57. break;
  58. }
  59. }
  60. function respond_to_list($inst_var) {
  61. // $inst_var = instance_variable_get("@#{inst_var_name}")
  62. // global $$inst_var_name;
  63. // $inst_var = &$$inst_var_name;
  64. switch (Request::$format) {
  65. case 'json':
  66. if (method_exists($inst_var, 'to_json'))
  67. render('json', $inst_var->to_json());
  68. else
  69. render('json', to_json($inst_var));
  70. break;
  71. case 'xml':
  72. break;
  73. }
  74. }
  75. function redirect_homepage() {
  76. // if(!CONFIG::show_homepage)
  77. // redirect_to('controller=>post', 'action=>index');
  78. }
  79. function numbers_to_imoutos($number){
  80. if(!CONFIG::show_homepage_imoutos)
  81. return;
  82. $number = str_split($number);
  83. $output = '<div style="margin-bottom: 1em;">'."\r\n";
  84. foreach($number as $num)
  85. $output .= ' <img alt="' . $num . '" src="/images/' . $num . ".gif\" />\r\n";
  86. $output .= " </div>\r\n";
  87. return $output;
  88. }
  89. function init_cookies() {
  90. if(Request::$format != 'html' || Request::$format == 'json')
  91. return;
  92. // $forum_posts = ForumPost::find('all', array('order' => "updated_at DESC", 'limit' => 10, 'conditions' => "parent_id IS NULL"));
  93. // foreach($forum_posts as $fp) {
  94. // $updated = User::$current->is_anonymous ? false : $fp->updated_at > User::$current->last_forum_topic_read_at;
  95. // $fp_cookies[] = array($fp->updated_at, $fp->id, $updated, ceil($fp->response_count/30));
  96. // }
  97. // Cookies::$list["current_forum_posts"] = to_json($fp_cookies);
  98. // Cookies::$list["country"] = $current_user_country;
  99. // vde(User::$current->is_anonymous);
  100. if(!User::$current->is_anonymous) {
  101. cookie_put("user_id", (string)User::$current->id);
  102. cookie_put("user_info", User::$current->user_info_cookie());
  103. // Cookies::$list["has_mail"] = User::$current->has_mail() ? "1" : "0";
  104. cookie_put("forum_updated", User::$current->is(">=30") && ForumPost::updated(User::$current) ? "1" : "0");
  105. // Cookies::$list["comments_updated"] = User::$current->is(">=30") && Comment::updated(User::$current) ? "1" : "0";
  106. // if(User::$current->is(">=35")) {
  107. // $mod_pending = Post::count(array('conditions' => array("status = 'flagged' OR status = 'pending'")));
  108. // cookies["mod_pending"] = $mod_pending;
  109. // }
  110. // if(User::$current->is_blocked()) {
  111. // if(User::$current->ban)
  112. // Cookies::$list["block_reason"] = "You have been blocked. Reason: ".User::$current->ban->reason.". Expires: ".substr(User::$current->ban->expires_at, 0, 10);
  113. // else
  114. // Cookies::$list["block_reason"] = "You have been blocked.";
  115. // } else
  116. // Cookies::$list["block_reason"] = "";
  117. cookie_put("resize_image", User::$current->always_resize_images ? "1" : "0");
  118. cookie_put('show_advanced_editing', User::$current->show_advanced_editing ? "1" : "0" );
  119. // Cookies::$list["my_tags"] = User::$current->my_tags;
  120. // $a = explode("\r\n", User::$current->blacklisted_tags());
  121. // vde($a);
  122. cookie_rawput('blacklisted_tags', str_replace('%0D%0A', '&', urlencode(User::$current->blacklisted_tags())));
  123. // ["blacklisted_tags"] = User::$current->blacklisted_tags_array;
  124. cookie_put("held_post_count", User::$current->held_post_count());
  125. } else {
  126. cookie_remove('user_info');
  127. cookie_remove('login');
  128. // Cookies::$list["blacklisted_tags"] = str_replace('%0D%0A', '&', urlencode(implode("\r\n", CONFIG::$default_blacklists)));
  129. // Cookies::rawput
  130. cookie_rawput('blacklisted_tags', str_replace('%0D%0A', '&', urlencode(implode("\r\n", CONFIG::$default_blacklists))));
  131. }
  132. // if flash[:notice] then
  133. // cookies["notice"] = flash[:notice]
  134. }
  135. function save_tags_to_cookie() {
  136. if (!empty(Request::$params->tags))
  137. $tags = Request::$params->tags;
  138. elseif (!empty(Request::$params->post) && !empty(Request::$params->post['tags']))
  139. $tags = explode(' ', strtolower(Request::$params->post['tags']));
  140. else
  141. return;
  142. $tags = TagAlias::to_aliased($tags);
  143. if (!empty($_COOKIE["recent_tags"]))
  144. $tags = array_merge($tags, explode(' ', $_COOKIE["recent_tags"]));
  145. $tags = array_unique(array_filter($tags));
  146. cookie_put("recent_tags", implode(' ', array_slice($tags, 0, 20)));
  147. }
  148. function set_current_user() {
  149. $AnonymousUser = array(
  150. 'id' => null,
  151. 'level' => 0,
  152. 'name' => "Anonymous",
  153. 'pretty_name' => "Anonymous",
  154. 'is_anonymous' => true,
  155. 'show_samples' => true,
  156. 'has_avatar' => false,
  157. 'language' => '',
  158. 'secondary_languages' => '',
  159. 'secondary_language_array' => array(),
  160. 'ip_addr' => $_SERVER['REMOTE_ADDR'],
  161. 'pool_browse_mode' => 1
  162. );
  163. // if(!empty(User::$current)) {
  164. if (!empty($_SESSION[CONFIG::app_name]['user_id']))
  165. User::$current = User::find($_SESSION[CONFIG::app_name]['user_id']);
  166. // User::$current = new User('find', $_SESSION[CONFIG::app_name]['user_id']);
  167. elseif (isset($_COOKIE['login']) && isset($_COOKIE['pass_hash']))
  168. User::$current = User::authenticate_hash($_COOKIE['login'], $_COOKIE['pass_hash']);
  169. elseif (isset(Request::$params->login) && isset(Request::$params->password_hash))
  170. User::$current = User::authenticate(Request::$params->login, Request::$params->password_hash);
  171. elseif (isset(Request::$params->user['name']) && isset(Request::$params->user['password']))
  172. User::$current = User::authenticate(Request::$params->user['name'], Request::$params->user['password']);
  173. // vde(User::$current);
  174. if(User::$current) {
  175. # TODO:
  176. // if(User::$current->is_blocked && User::$current->ban && User::$current->ban->expires_at < gmd()) {
  177. // User::$current->update_attribute(array('level'->CONFIG["starting_level"]));
  178. // Ban::destroy_all("user_id = #{@current_user.id}")
  179. // }
  180. } else
  181. User::$current = User::create_from_array($AnonymousUser);
  182. // User::$current = new User('from_array', $AnonymousUser);
  183. // vde(User::$current);
  184. }
  185. function access_denied($page = '/user/login'){
  186. $previous_url = !empty(Request::$params->url) ? Request::$params->url : Request::$url;
  187. switch (Request::$format) {
  188. case 'html':
  189. notice('Access denied');
  190. redirect_to('user#login', array('url' => $previous_url));
  191. break;
  192. case 'json':
  193. render('json', array('success' => false, 'reason' => 'access denied'), array('status' => 403));
  194. break;
  195. case 'xml':
  196. render('xml', array('success' => false, 'reason' => 'access denied'),array('status' => 403));
  197. break;
  198. }
  199. // respond_to(array(
  200. // 'html' => array('notice' => "Access denied", 'redirect_to' => array('user#login', array('url' => $previous_url))),
  201. // 'json' => array('render' => array('json' => array('success' => false, 'reason' => 'access denied'), 'status' => 403))
  202. //'xml' => array('render' => array('xml' => array('success' => false, 'reason' => 'access denied'), 'status' => 403))
  203. // ));
  204. }
  205. ?>