PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/private/modules/socialfeed/helpers/zest.php

https://github.com/xig/SocialFeed
PHP | 272 lines | 234 code | 27 blank | 11 comment | 29 complexity | 4466f210a387eb84ad4f200748d6d41c MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. class zest_Core {
  3. public static function check_login() {
  4. return login::check_login();
  5. }
  6. public static function update_status($str) {
  7. $bitly = new Url_Shortner(array("login" => ORM::factory('setting','bitly_login')->value,"key" => ORM::factory('setting','bitly_api')->value));
  8. $str = $bitly->string_shorten($str);
  9. $twitter = Twitter::instance(ORM::factory('setting','twitter_username')->value,ORM::factory('setting','twitter_password')->value);
  10. $twitter->update_status($str);
  11. }
  12. public static function parse_user_agent($string, $key)
  13. {
  14. // Return the raw string
  15. if ($key === 'agent')
  16. return $string;
  17. // Parse the user agent and extract basic information
  18. $agents = Kohana::config('user_agents');
  19. foreach ($agents as $type => $data)
  20. {
  21. foreach ($data as $agent => $name)
  22. {
  23. if (stripos($string, $agent) !== FALSE)
  24. {
  25. if ($type === 'browser' AND preg_match('|'.preg_quote($agent).'[^0-9.]*+([0-9.][0-9.a-z]*)|i', $string, $match))
  26. {
  27. // Set the browser version
  28. $info['version'] = $match[1];
  29. }
  30. // Set the agent name
  31. $info[$type] = $name;
  32. break;
  33. }
  34. }
  35. }
  36. if (empty($info[$key]))
  37. {
  38. switch ($key)
  39. {
  40. case 'is_robot':
  41. case 'is_browser':
  42. case 'is_mobile':
  43. // A boolean result
  44. $return = ! empty($info[substr($key, 3)]);
  45. break;
  46. break;
  47. }
  48. // Cache the return value
  49. isset($return) and $info[$key] = $return;
  50. }
  51. // Return the key, if set
  52. return isset($info[$key]) ? $info[$key] : NULL;
  53. }
  54. public static function render_image($media, $options = array()) {
  55. if (isset($options['width']) && isset($options['height'])) {
  56. if (isset($options['thickbox']))
  57. return '<a href="'.url::base().'assets/images/'.$media->filename.'" class="thickbox" title="'.$media->name.'"><img src="'.image_helper::render_crop($media->filename,$options['width'],$options['height']).'" alt="'.$media->name.'" /></a>';
  58. else
  59. return '<img src="'.image_helper::render_crop($media->filename,$options['width'],$options['height']).'" alt="'.$media->name.'" />';
  60. }
  61. else
  62. return '<img src="/assets/images/'.$media->filename.'" alt="'.$media->name.'" />';
  63. }
  64. public static function add_this($url,$title) {
  65. $url = 'http://'.$_SERVER['HTTP_HOST'].$url;
  66. return '<div><script type="text/javascript">var addthis_pub="sydlawrence";</script>
  67. <a href="http://www.addthis.com/bookmark.php?v=20" onmouseover="return addthis_open(this, \'\', \''.$url.'\', \''.$title.'\')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s7.addthis.com/static/btn/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/200/addthis_widget.js"></script></div>';
  68. }
  69. public static function get_page_tree($parent = 0, $level = 0, $active = true) {
  70. $array = array();
  71. // retrieve all children of $parent
  72. if ($active)
  73. $pages = ORM::factory('page')->where(array('parent_id' => $parent,'status_id'=>2))->orderby(array('navbar_id'=>'asc','order'=>'asc'))->find_all();
  74. else
  75. $pages = ORM::factory('page')->where('parent_id', $parent)->orderby(array('navbar_id'=>'asc','order'=>'asc'))->find_all();
  76. foreach ($pages as $page) {
  77. $array[] = array($page,self::get_page_tree($page->id, $level+1, $active));
  78. }
  79. return $array;
  80. }
  81. public static function dir_to_array($directory) {
  82. $handler = opendir($directory);
  83. // keep going until all files in directory have been read
  84. $array = array();
  85. while ($file = readdir($handler)) {
  86. // if $file isn't this directory or its parent,
  87. // add it to the results array
  88. if ($file != '.' && $file != '..') {
  89. $array[] = $file;
  90. }
  91. }
  92. closedir($handler);
  93. sort($array);
  94. return $array;
  95. }
  96. public static function get_version($with_name = FALSE) {
  97. $html = "";
  98. $file_handle = fopen(DOCROOT.'/version.txt', "r");
  99. while (!feof($file_handle)) {
  100. $html .= fgets($file_handle);
  101. }
  102. fclose($file_handle);
  103. if ($with_name)
  104. return $html;
  105. else
  106. return str_replace('Zest CMS','',$html);
  107. }
  108. public static function template_to_html($filename) {
  109. if (file_exists(APPPATH.'/views/'.$filename)) {
  110. $html = '';
  111. $file_handle = fopen(APPPATH.'/views/'.$filename, "r");
  112. while (!feof($file_handle)) {
  113. $html .= fgets($file_handle);
  114. }
  115. fclose($file_handle);
  116. return $html;
  117. }
  118. else if (file_exists(APPPATH.'/views/'.$filename.'.zest')) {
  119. $html = '';
  120. $file_handle = fopen(APPPATH.'/views/'.$filename.'.zest', "r");
  121. while (!feof($file_handle)) {
  122. $html .= fgets($file_handle);
  123. }
  124. fclose($file_handle);
  125. return $html;
  126. }
  127. else if (file_exists(APPPATH.'/views/'.$filename.'.php')) {
  128. $html = '';
  129. $file_handle = fopen(APPPATH.'/views/'.$filename.'.php', "r");
  130. while (!feof($file_handle)) {
  131. $html .= fgets($file_handle);
  132. }
  133. fclose($file_handle);
  134. return $html;
  135. }
  136. else if (file_exists(DOCROOT.$filename)) {
  137. $html = '';
  138. $file_handle = fopen(DOCROOT.$filename,"r");
  139. while (!feof($file_handle)) {
  140. $html .= fgets($file_handle);
  141. }
  142. fclose($file_handle);
  143. return $html;
  144. }
  145. else {
  146. return $filename." does not exist!";
  147. }
  148. }
  149. public static function save_to_file($filename, $content) {
  150. $filename = APPPATH.'views/'.$filename;
  151. if (file_exists($filename)) {
  152. $fh = fopen($filename, 'w') or die("can't open file");
  153. fwrite($fh, $content);
  154. fclose($fh);
  155. return true;
  156. }
  157. return false;
  158. }
  159. public static function get_xml_entries($url) {
  160. $options = array(
  161. CURLOPT_RETURNTRANSFER => TRUE,
  162. CURLOPT_URL => $url
  163. );
  164. $curl = new Curl($options);
  165. $data = simplexml_load_string($curl->execute());
  166. $arr = array();
  167. foreach ($data->channel->item as $item) {
  168. $arr[] = $item;
  169. }
  170. return $arr;
  171. }
  172. public static function get_secure_xml_entries($url) {
  173. }
  174. public static function ping() {
  175. $url = "http://".$_SERVER['HTTP_HOST'].'/admin/ping/technorati';
  176. $options = array(
  177. CURLOPT_RETURNTRANSFER => TRUE,
  178. CURLOPT_URL => $url
  179. );
  180. $curl = new Curl($options);
  181. $curl->execute();
  182. }
  183. public static function email($email)
  184. {
  185. return (bool) preg_match('/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD', (string) $email);
  186. }
  187. public static function add_stat($type,$id) {
  188. if (Kohana::user_agent('is_browser'))
  189. ORM::factory('statistic')->add_view($type,$id);
  190. }
  191. public static function button($href,$title,$options) {
  192. $options = arr::merge($options, array('class'=>'button'));
  193. return html::anchor($href, $title, $options);
  194. }
  195. public static function relative_time($date) {
  196. $diff = time() - strtotime($date);
  197. if ($diff>0) {
  198. if ($diff<60)
  199. return $diff . " " . inflector::plural("second",$diff) . " ago";
  200. $diff = round($diff/60);
  201. if ($diff<60)
  202. return $diff . " " . inflector::plural("minute",$diff) . " ago";
  203. $diff = round($diff/60);
  204. if ($diff<24)
  205. return $diff . " " . inflector::plural("hour",$diff) . " ago";
  206. $diff = round($diff/24);
  207. if ($diff<7)
  208. return $diff . " " . inflector::plural("day",$diff) . " ago";
  209. $diff = round($diff/7);
  210. if ($diff<4)
  211. return $diff . " " . inflector::plural("week",$diff) . " ago";
  212. return date("d/m/Y", strtotime($date));
  213. } else {
  214. if ($diff>-60)
  215. return "in " . -$diff . " " . inflector::plural("second",$diff);
  216. $diff = round($diff/60);
  217. if ($diff>-60)
  218. return "in " . -$diff . " " . inflector::plural("minute",$diff);
  219. $diff = round($diff/60);
  220. if ($diff>-24)
  221. return "in " . -$diff . " " . inflector::plural("hour",$diff);
  222. $diff = round($diff/24);
  223. if ($diff>-7)
  224. return "in " . -$diff . " " . inflector::plural("day",$diff);
  225. $diff = round($diff/7);
  226. if ($diff>-4)
  227. return "in " . -$diff . " " . inflector::plural("week",$diff);
  228. return date("d/m/Y", strtotime($date));
  229. }
  230. }
  231. }